blob: 36cb74765f3b9f06e740207992fba67fdc0cfd2f [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,
Artyom Skrobove9b3fb82015-12-07 14:22:39 +000082 StringRef StringValue) override;
Craig Topperca7e3e52014-03-10 03:19:03 +000083 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) {
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000198 OS << "\t.arch\t" << ARM::getArchName(Arch) << "\n";
Logan Chien439e8f92013-12-11 17:16:25 +0000199}
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +0000200void ARMTargetAsmStreamer::emitArchExtension(unsigned ArchExt) {
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000201 OS << "\t.arch_extension\t" << ARM::getArchExtName(ArchExt) << "\n";
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +0000202}
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000203void ARMTargetAsmStreamer::emitObjectArch(unsigned Arch) {
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000204 OS << "\t.object_arch\t" << ARM::getArchName(Arch) << '\n';
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000205}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000206void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000207 OS << "\t.fpu\t" << ARM::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) {
Matt Arsenault8b643552015-06-09 00:31:39 +0000217 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
218
219 OS << "\t.thumb_set\t";
220 Symbol->print(OS, MAI);
221 OS << ", ";
222 Value->print(OS, MAI);
223 OS << '\n';
Rafael Espindola466d6632014-04-27 20:23:58 +0000224}
225
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000226void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
227 OS << "\t.inst";
228 if (Suffix)
229 OS << "." << Suffix;
Benjamin Kramerbe48c402015-05-23 16:39:10 +0000230 OS << "\t0x" << Twine::utohexstr(Inst) << "\n";
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000231}
232
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000233void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
234 const SmallVectorImpl<uint8_t> &Opcodes) {
235 OS << "\t.unwind_raw " << Offset;
236 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
237 OCE = Opcodes.end();
238 OCI != OCE; ++OCI)
Benjamin Kramerbe48c402015-05-23 16:39:10 +0000239 OS << ", 0x" << Twine::utohexstr(*OCI);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000240 OS << '\n';
241}
242
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000243class ARMTargetELFStreamer : public ARMTargetStreamer {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000244private:
245 // This structure holds all attributes, accounting for
Artyom Skrobove9b3fb82015-12-07 14:22:39 +0000246 // their string/numeric value, so we can later emit them
Logan Chien8cbb80d2013-10-28 17:51:12 +0000247 // in declaration order, keeping all in the same vector
248 struct AttributeItem {
249 enum {
250 HiddenAttribute = 0,
251 NumericAttribute,
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000252 TextAttribute,
253 NumericAndTextAttributes
Logan Chien8cbb80d2013-10-28 17:51:12 +0000254 } Type;
255 unsigned Tag;
256 unsigned IntValue;
Artyom Skrobove9b3fb82015-12-07 14:22:39 +0000257 std::string StringValue;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000258
259 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
Charlie Turner8b2caa42015-01-05 13:12:17 +0000260 // The conformance tag must be emitted first when serialised
261 // into an object file. Specifically, the addenda to the ARM ABI
262 // states that (2.3.7.4):
263 //
264 // "To simplify recognition by consumers in the common case of
265 // claiming conformity for the whole file, this tag should be
266 // emitted first in a file-scope sub-subsection of the first
267 // public subsection of the attributes section."
268 //
269 // So it is special-cased in this comparison predicate when the
270 // attributes are sorted in finishAttributeSection().
271 return (RHS.Tag != ARMBuildAttrs::conformance) &&
272 ((LHS.Tag == ARMBuildAttrs::conformance) || (LHS.Tag < RHS.Tag));
Logan Chien8cbb80d2013-10-28 17:51:12 +0000273 }
274 };
275
276 StringRef CurrentVendor;
277 unsigned FPU;
Logan Chien439e8f92013-12-11 17:16:25 +0000278 unsigned Arch;
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000279 unsigned EmittedArch;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000280 SmallVector<AttributeItem, 64> Contents;
281
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000282 MCSection *AttributeSection;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000283
Logan Chien8cbb80d2013-10-28 17:51:12 +0000284 AttributeItem *getAttributeItem(unsigned Attribute) {
285 for (size_t i = 0; i < Contents.size(); ++i)
286 if (Contents[i].Tag == Attribute)
287 return &Contents[i];
Craig Topper062a2ba2014-04-25 05:30:21 +0000288 return nullptr;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000289 }
290
291 void setAttributeItem(unsigned Attribute, unsigned Value,
292 bool OverwriteExisting) {
293 // Look for existing attribute item
294 if (AttributeItem *Item = getAttributeItem(Attribute)) {
295 if (!OverwriteExisting)
296 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000297 Item->Type = AttributeItem::NumericAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000298 Item->IntValue = Value;
299 return;
300 }
301
302 // Create new attribute item
303 AttributeItem Item = {
304 AttributeItem::NumericAttribute,
305 Attribute,
306 Value,
307 StringRef("")
308 };
309 Contents.push_back(Item);
310 }
311
312 void setAttributeItem(unsigned Attribute, StringRef Value,
313 bool OverwriteExisting) {
314 // Look for existing attribute item
315 if (AttributeItem *Item = getAttributeItem(Attribute)) {
316 if (!OverwriteExisting)
317 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000318 Item->Type = AttributeItem::TextAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000319 Item->StringValue = Value;
320 return;
321 }
322
323 // Create new attribute item
324 AttributeItem Item = {
325 AttributeItem::TextAttribute,
326 Attribute,
327 0,
328 Value
329 };
330 Contents.push_back(Item);
331 }
332
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000333 void setAttributeItems(unsigned Attribute, unsigned IntValue,
334 StringRef StringValue, bool OverwriteExisting) {
335 // Look for existing attribute item
336 if (AttributeItem *Item = getAttributeItem(Attribute)) {
337 if (!OverwriteExisting)
338 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000339 Item->Type = AttributeItem::NumericAndTextAttributes;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000340 Item->IntValue = IntValue;
341 Item->StringValue = StringValue;
342 return;
343 }
344
345 // Create new attribute item
346 AttributeItem Item = {
347 AttributeItem::NumericAndTextAttributes,
348 Attribute,
349 IntValue,
350 StringValue
351 };
352 Contents.push_back(Item);
353 }
354
Logan Chien439e8f92013-12-11 17:16:25 +0000355 void emitArchDefaultAttributes();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000356 void emitFPUDefaultAttributes();
357
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000358 ARMELFStreamer &getStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000359
Craig Topperca7e3e52014-03-10 03:19:03 +0000360 void emitFnStart() override;
361 void emitFnEnd() override;
362 void emitCantUnwind() override;
363 void emitPersonality(const MCSymbol *Personality) override;
364 void emitPersonalityIndex(unsigned Index) override;
365 void emitHandlerData() override;
366 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
367 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
368 void emitPad(int64_t Offset) override;
369 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
370 bool isVector) override;
371 void emitUnwindRaw(int64_t Offset,
372 const SmallVectorImpl<uint8_t> &Opcodes) override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000373
Craig Topperca7e3e52014-03-10 03:19:03 +0000374 void switchVendor(StringRef Vendor) override;
375 void emitAttribute(unsigned Attribute, unsigned Value) override;
376 void emitTextAttribute(unsigned Attribute, StringRef String) override;
377 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
378 StringRef StringValue) override;
379 void emitArch(unsigned Arch) override;
380 void emitObjectArch(unsigned Arch) override;
381 void emitFPU(unsigned FPU) override;
382 void emitInst(uint32_t Inst, char Suffix = '\0') override;
383 void finishAttributeSection() override;
Rafael Espindola4c6f6132014-04-27 17:10:46 +0000384 void emitLabel(MCSymbol *Symbol) override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000385
Craig Topperca7e3e52014-03-10 03:19:03 +0000386 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
Rafael Espindola466d6632014-04-27 20:23:58 +0000387 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000388
Logan Chien8cbb80d2013-10-28 17:51:12 +0000389 size_t calculateContentSize() const;
390
Keno Fischer00021422016-01-12 13:38:15 +0000391 // Reset state between object emissions
392 void reset() override;
393
Logan Chien8cbb80d2013-10-28 17:51:12 +0000394public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000395 ARMTargetELFStreamer(MCStreamer &S)
Renato Golin35de35d2015-05-12 10:33:58 +0000396 : ARMTargetStreamer(S), CurrentVendor("aeabi"), FPU(ARM::FK_INVALID),
397 Arch(ARM::AK_INVALID), EmittedArch(ARM::AK_INVALID),
Craig Topper062a2ba2014-04-25 05:30:21 +0000398 AttributeSection(nullptr) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000399};
400
Tim Northover5cc3dc82012-12-07 16:50:23 +0000401/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
402/// the appropriate points in the object files. These symbols are defined in the
403/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
404///
405/// In brief: $a, $t or $d should be emitted at the start of each contiguous
406/// region of ARM code, Thumb code or data in a section. In practice, this
407/// emission does not rely on explicit assembler directives but on inherent
408/// properties of the directives doing the emission (e.g. ".byte" is data, "add
409/// r0, r0, r0" an instruction).
410///
411/// As a result this system is orthogonal to the DataRegion infrastructure used
412/// by MachO. Beware!
413class ARMELFStreamer : public MCELFStreamer {
414public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000415 friend class ARMTargetELFStreamer;
416
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000417 ARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_pwrite_stream &OS,
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000418 MCCodeEmitter *Emitter, bool IsThumb)
419 : MCELFStreamer(Context, TAB, OS, Emitter), IsThumb(IsThumb),
420 MappingSymbolCounter(0), LastEMS(EMS_None) {
Keno Fischer00021422016-01-12 13:38:15 +0000421 EHReset();
Logan Chiend8bb4b72013-04-16 12:02:21 +0000422 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000423
424 ~ARMELFStreamer() {}
425
Craig Topperca7e3e52014-03-10 03:19:03 +0000426 void FinishImpl() override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000427
Logan Chien2bcc42c2013-01-30 15:39:04 +0000428 // ARM exception handling directives
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000429 void emitFnStart();
430 void emitFnEnd();
431 void emitCantUnwind();
432 void emitPersonality(const MCSymbol *Per);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000433 void emitPersonalityIndex(unsigned index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000434 void emitHandlerData();
435 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000436 void emitMovSP(unsigned Reg, int64_t Offset = 0);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000437 void emitPad(int64_t Offset);
438 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000439 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000440
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000441 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000442 // We have to keep track of the mapping symbol state of any sections we
443 // use. Each one should start off as EMS_None, which is provided as the
444 // default constructor by DenseMap::lookup.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000445 LastMappingSymbols[getPreviousSection().first] = LastEMS;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000446 LastEMS = LastMappingSymbols.lookup(Section);
447
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000448 MCELFStreamer::ChangeSection(Section, Subsection);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000449 }
450
451 /// This function is the one used to emit instruction data into the ELF
452 /// streamer. We override it to add the appropriate mapping symbol if
453 /// necessary.
Craig Topperca7e3e52014-03-10 03:19:03 +0000454 void EmitInstruction(const MCInst& Inst,
455 const MCSubtargetInfo &STI) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000456 if (IsThumb)
457 EmitThumbMappingSymbol();
458 else
459 EmitARMMappingSymbol();
460
David Woodhousee6c13e42014-01-28 23:12:42 +0000461 MCELFStreamer::EmitInstruction(Inst, STI);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000462 }
463
Craig Topperd25ff6f2014-03-10 03:22:59 +0000464 void emitInst(uint32_t Inst, char Suffix) {
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000465 unsigned Size;
466 char Buffer[4];
467 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
468
469 switch (Suffix) {
470 case '\0':
471 Size = 4;
472
473 assert(!IsThumb);
474 EmitARMMappingSymbol();
475 for (unsigned II = 0, IE = Size; II != IE; II++) {
476 const unsigned I = LittleEndian ? (Size - II - 1) : II;
477 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
478 }
479
480 break;
481 case 'n':
482 case 'w':
483 Size = (Suffix == 'n' ? 2 : 4);
484
485 assert(IsThumb);
486 EmitThumbMappingSymbol();
487 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
488 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
489 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
490 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
491 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
492 }
493
494 break;
495 default:
496 llvm_unreachable("Invalid Suffix");
497 }
498
499 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
500 }
501
Tim Northover5cc3dc82012-12-07 16:50:23 +0000502 /// 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.
Craig Topperca7e3e52014-03-10 03:19:03 +0000505 void EmitBytes(StringRef Data) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000506 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000507 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000508 }
509
510 /// This is one of the functions used to emit data into an ELF section, so the
511 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
512 /// necessary.
Craig Topper3c76c522015-09-20 23:35:59 +0000513 void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
Saleem Abdulrasoolfe781972015-01-11 04:39:18 +0000514 if (const MCSymbolRefExpr *SRE = dyn_cast_or_null<MCSymbolRefExpr>(Value))
Oliver Stannard9be59af2015-11-17 10:00:43 +0000515 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_SBREL && !(Size == 4)) {
516 getContext().reportError(Loc, "relocated expression must be 32-bit");
517 return;
518 }
Saleem Abdulrasoolfe781972015-01-11 04:39:18 +0000519
Tim Northover5cc3dc82012-12-07 16:50:23 +0000520 EmitDataMappingSymbol();
Oliver Stannard09be0602015-11-16 16:22:47 +0000521 MCELFStreamer::EmitValueImpl(Value, Size, Loc);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000522 }
523
Craig Topperca7e3e52014-03-10 03:19:03 +0000524 void EmitAssemblerFlag(MCAssemblerFlag Flag) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000525 MCELFStreamer::EmitAssemblerFlag(Flag);
526
527 switch (Flag) {
528 case MCAF_SyntaxUnified:
529 return; // no-op here.
530 case MCAF_Code16:
531 IsThumb = true;
532 return; // Change to Thumb mode
533 case MCAF_Code32:
534 IsThumb = false;
535 return; // Change to ARM mode
536 case MCAF_Code64:
537 return;
538 case MCAF_SubsectionsViaSymbols:
539 return;
540 }
541 }
542
543private:
544 enum ElfMappingSymbol {
545 EMS_None,
546 EMS_ARM,
547 EMS_Thumb,
548 EMS_Data
549 };
550
551 void EmitDataMappingSymbol() {
552 if (LastEMS == EMS_Data) return;
553 EmitMappingSymbol("$d");
554 LastEMS = EMS_Data;
555 }
556
557 void EmitThumbMappingSymbol() {
558 if (LastEMS == EMS_Thumb) return;
559 EmitMappingSymbol("$t");
560 LastEMS = EMS_Thumb;
561 }
562
563 void EmitARMMappingSymbol() {
564 if (LastEMS == EMS_ARM) return;
565 EmitMappingSymbol("$a");
566 LastEMS = EMS_ARM;
567 }
568
569 void EmitMappingSymbol(StringRef Name) {
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000570 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
571 Name + "." + Twine(MappingSymbolCounter++)));
Rafael Espindolac5fb5082015-06-26 11:31:13 +0000572 EmitLabel(Symbol);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000573
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000574 Symbol->setType(ELF::STT_NOTYPE);
575 Symbol->setBinding(ELF::STB_LOCAL);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000576 Symbol->setExternal(false);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000577 }
578
Craig Topperca7e3e52014-03-10 03:19:03 +0000579 void EmitThumbFunc(MCSymbol *Func) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000580 getAssembler().setIsThumbFunc(Func);
Rafael Espindolab60c8292014-04-29 12:46:50 +0000581 EmitSymbolAttribute(Func, MCSA_ELF_TypeFunction);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000582 }
583
Logan Chien2bcc42c2013-01-30 15:39:04 +0000584 // Helper functions for ARM exception handling directives
Keno Fischer00021422016-01-12 13:38:15 +0000585 void EHReset();
586
587 // Reset state between object emissions
588 void reset() override;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000589
590 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000591 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000592 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000593
594 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
595 SectionKind Kind, const MCSymbol &Fn);
596 void SwitchToExTabSection(const MCSymbol &FnStart);
597 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000598
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000599 void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
600
Tim Northover5cc3dc82012-12-07 16:50:23 +0000601 bool IsThumb;
602 int64_t MappingSymbolCounter;
603
604 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
605 ElfMappingSymbol LastEMS;
606
Logan Chien2bcc42c2013-01-30 15:39:04 +0000607 // ARM Exception Handling Frame Information
608 MCSymbol *ExTab;
609 MCSymbol *FnStart;
610 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000611 unsigned PersonalityIndex;
612 unsigned FPReg; // Frame pointer register
613 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
614 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
615 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000616 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000617 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000618 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000619 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000620};
Logan Chiend8bb4b72013-04-16 12:02:21 +0000621} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000622
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000623ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000624 return static_cast<ARMELFStreamer &>(Streamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000625}
626
627void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
628void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
629void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
630void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
631 getStreamer().emitPersonality(Personality);
632}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000633void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
634 getStreamer().emitPersonalityIndex(Index);
635}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000636void ARMTargetELFStreamer::emitHandlerData() {
637 getStreamer().emitHandlerData();
638}
639void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
640 int64_t Offset) {
641 getStreamer().emitSetFP(FpReg, SpReg, Offset);
642}
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000643void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
644 getStreamer().emitMovSP(Reg, Offset);
645}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000646void ARMTargetELFStreamer::emitPad(int64_t Offset) {
647 getStreamer().emitPad(Offset);
648}
649void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
650 bool isVector) {
651 getStreamer().emitRegSave(RegList, isVector);
652}
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000653void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
654 const SmallVectorImpl<uint8_t> &Opcodes) {
655 getStreamer().emitUnwindRaw(Offset, Opcodes);
656}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000657void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
658 assert(!Vendor.empty() && "Vendor cannot be empty.");
659
660 if (CurrentVendor == Vendor)
661 return;
662
663 if (!CurrentVendor.empty())
664 finishAttributeSection();
665
666 assert(Contents.empty() &&
667 ".ARM.attributes should be flushed before changing vendor");
668 CurrentVendor = Vendor;
669
670}
671void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
672 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
673}
674void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
675 StringRef Value) {
676 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
677}
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000678void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
679 unsigned IntValue,
680 StringRef StringValue) {
681 setAttributeItems(Attribute, IntValue, StringValue,
682 /* OverwriteExisting= */ true);
683}
Logan Chien439e8f92013-12-11 17:16:25 +0000684void ARMTargetELFStreamer::emitArch(unsigned Value) {
685 Arch = Value;
686}
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000687void ARMTargetELFStreamer::emitObjectArch(unsigned Value) {
688 EmittedArch = Value;
689}
Logan Chien439e8f92013-12-11 17:16:25 +0000690void ARMTargetELFStreamer::emitArchDefaultAttributes() {
691 using namespace ARMBuildAttrs;
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000692
Renato Golinf5f373f2015-05-08 21:04:27 +0000693 setAttributeItem(CPU_name,
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000694 ARM::getCPUAttr(Arch),
Renato Golinf5f373f2015-05-08 21:04:27 +0000695 false);
696
Renato Golin35de35d2015-05-12 10:33:58 +0000697 if (EmittedArch == ARM::AK_INVALID)
Renato Golinf5f373f2015-05-08 21:04:27 +0000698 setAttributeItem(CPU_arch,
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000699 ARM::getArchAttr(Arch),
Renato Golinf5f373f2015-05-08 21:04:27 +0000700 false);
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000701 else
Renato Golinf5f373f2015-05-08 21:04:27 +0000702 setAttributeItem(CPU_arch,
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000703 ARM::getArchAttr(EmittedArch),
Renato Golinf5f373f2015-05-08 21:04:27 +0000704 false);
Logan Chien439e8f92013-12-11 17:16:25 +0000705
706 switch (Arch) {
Renato Golin35de35d2015-05-12 10:33:58 +0000707 case ARM::AK_ARMV2:
708 case ARM::AK_ARMV2A:
709 case ARM::AK_ARMV3:
710 case ARM::AK_ARMV3M:
711 case ARM::AK_ARMV4:
Logan Chien439e8f92013-12-11 17:16:25 +0000712 setAttributeItem(ARM_ISA_use, Allowed, false);
713 break;
714
Renato Golin35de35d2015-05-12 10:33:58 +0000715 case ARM::AK_ARMV4T:
716 case ARM::AK_ARMV5T:
717 case ARM::AK_ARMV5TE:
718 case ARM::AK_ARMV6:
Logan Chien439e8f92013-12-11 17:16:25 +0000719 setAttributeItem(ARM_ISA_use, Allowed, false);
720 setAttributeItem(THUMB_ISA_use, Allowed, false);
721 break;
722
Renato Golin35de35d2015-05-12 10:33:58 +0000723 case ARM::AK_ARMV6T2:
Logan Chien439e8f92013-12-11 17:16:25 +0000724 setAttributeItem(ARM_ISA_use, Allowed, false);
725 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
726 break;
727
Renato Golin35de35d2015-05-12 10:33:58 +0000728 case ARM::AK_ARMV6K:
Artyom Skrobovf187a652015-11-16 14:05:32 +0000729 case ARM::AK_ARMV6KZ:
Logan Chien439e8f92013-12-11 17:16:25 +0000730 setAttributeItem(ARM_ISA_use, Allowed, false);
731 setAttributeItem(THUMB_ISA_use, Allowed, false);
732 setAttributeItem(Virtualization_use, AllowTZ, false);
733 break;
734
Renato Golin35de35d2015-05-12 10:33:58 +0000735 case ARM::AK_ARMV6M:
Logan Chien439e8f92013-12-11 17:16:25 +0000736 setAttributeItem(THUMB_ISA_use, Allowed, false);
737 break;
738
Renato Golin35de35d2015-05-12 10:33:58 +0000739 case ARM::AK_ARMV7A:
Logan Chien439e8f92013-12-11 17:16:25 +0000740 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
741 setAttributeItem(ARM_ISA_use, Allowed, false);
742 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
743 break;
744
Renato Golin35de35d2015-05-12 10:33:58 +0000745 case ARM::AK_ARMV7R:
Logan Chien439e8f92013-12-11 17:16:25 +0000746 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
747 setAttributeItem(ARM_ISA_use, Allowed, false);
748 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
749 break;
750
Renato Golin35de35d2015-05-12 10:33:58 +0000751 case ARM::AK_ARMV7M:
Logan Chien439e8f92013-12-11 17:16:25 +0000752 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
753 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
754 break;
755
Renato Golin35de35d2015-05-12 10:33:58 +0000756 case ARM::AK_ARMV8A:
757 case ARM::AK_ARMV8_1A:
Oliver Stannard46670712015-12-01 10:33:56 +0000758 case ARM::AK_ARMV8_2A:
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
Bradley Smithe26f7992016-01-15 10:24:39 +0000766 case ARM::AK_ARMV8MBaseline:
767 case ARM::AK_ARMV8MMainline:
768 setAttributeItem(THUMB_ISA_use, AllowThumbDerived, false);
769 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
770 break;
771
Renato Golin35de35d2015-05-12 10:33:58 +0000772 case ARM::AK_IWMMXT:
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, AllowWMMXv1, false);
776 break;
777
Renato Golin35de35d2015-05-12 10:33:58 +0000778 case ARM::AK_IWMMXT2:
Logan Chien439e8f92013-12-11 17:16:25 +0000779 setAttributeItem(ARM_ISA_use, Allowed, false);
780 setAttributeItem(THUMB_ISA_use, Allowed, false);
781 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
782 break;
783
784 default:
785 report_fatal_error("Unknown Arch: " + Twine(Arch));
786 break;
787 }
788}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000789void ARMTargetELFStreamer::emitFPU(unsigned Value) {
790 FPU = Value;
791}
792void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
793 switch (FPU) {
Renato Golin35de35d2015-05-12 10:33:58 +0000794 case ARM::FK_VFP:
795 case ARM::FK_VFPV2:
Logan Chiena39510a2013-12-18 17:23:15 +0000796 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000797 ARMBuildAttrs::AllowFPv2,
798 /* OverwriteExisting= */ false);
799 break;
800
Renato Golin35de35d2015-05-12 10:33:58 +0000801 case ARM::FK_VFPV3:
Logan Chiena39510a2013-12-18 17:23:15 +0000802 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000803 ARMBuildAttrs::AllowFPv3A,
804 /* OverwriteExisting= */ false);
805 break;
806
Javed Absard5526302015-06-29 09:32:29 +0000807 case ARM::FK_VFPV3_FP16:
808 setAttributeItem(ARMBuildAttrs::FP_arch,
809 ARMBuildAttrs::AllowFPv3A,
810 /* OverwriteExisting= */ false);
811 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
812 ARMBuildAttrs::AllowHPFP,
813 /* OverwriteExisting= */ false);
814 break;
815
Renato Golin35de35d2015-05-12 10:33:58 +0000816 case ARM::FK_VFPV3_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000817 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000818 ARMBuildAttrs::AllowFPv3B,
819 /* OverwriteExisting= */ false);
820 break;
821
Javed Absard5526302015-06-29 09:32:29 +0000822 case ARM::FK_VFPV3_D16_FP16:
823 setAttributeItem(ARMBuildAttrs::FP_arch,
824 ARMBuildAttrs::AllowFPv3B,
825 /* OverwriteExisting= */ false);
826 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
827 ARMBuildAttrs::AllowHPFP,
828 /* OverwriteExisting= */ false);
829 break;
830
831 case ARM::FK_VFPV3XD:
832 setAttributeItem(ARMBuildAttrs::FP_arch,
833 ARMBuildAttrs::AllowFPv3B,
834 /* OverwriteExisting= */ false);
835 break;
836 case ARM::FK_VFPV3XD_FP16:
837 setAttributeItem(ARMBuildAttrs::FP_arch,
838 ARMBuildAttrs::AllowFPv3B,
839 /* OverwriteExisting= */ false);
840 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
841 ARMBuildAttrs::AllowHPFP,
842 /* OverwriteExisting= */ false);
843 break;
844
Renato Golin35de35d2015-05-12 10:33:58 +0000845 case ARM::FK_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000846 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000847 ARMBuildAttrs::AllowFPv4A,
848 /* OverwriteExisting= */ false);
849 break;
850
John Brawn985c04e2015-06-05 13:31:19 +0000851 // ABI_HardFP_use is handled in ARMAsmPrinter, so _SP_D16 is treated the same
852 // as _D16 here.
853 case ARM::FK_FPV4_SP_D16:
Renato Golin35de35d2015-05-12 10:33:58 +0000854 case ARM::FK_VFPV4_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000855 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000856 ARMBuildAttrs::AllowFPv4B,
857 /* OverwriteExisting= */ false);
858 break;
859
Renato Golin35de35d2015-05-12 10:33:58 +0000860 case ARM::FK_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000861 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000862 ARMBuildAttrs::AllowFPARMv8A,
863 /* OverwriteExisting= */ false);
864 break;
865
Oliver Stannard37e4daa2014-10-01 09:02:17 +0000866 // FPV5_D16 is identical to FP_ARMV8 except for the number of D registers, so
867 // uses the FP_ARMV8_D16 build attribute.
John Brawn985c04e2015-06-05 13:31:19 +0000868 case ARM::FK_FPV5_SP_D16:
Renato Golin35de35d2015-05-12 10:33:58 +0000869 case ARM::FK_FPV5_D16:
Oliver Stannard37e4daa2014-10-01 09:02:17 +0000870 setAttributeItem(ARMBuildAttrs::FP_arch,
871 ARMBuildAttrs::AllowFPARMv8B,
872 /* OverwriteExisting= */ false);
873 break;
874
Renato Golin35de35d2015-05-12 10:33:58 +0000875 case ARM::FK_NEON:
Logan Chiena39510a2013-12-18 17:23:15 +0000876 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000877 ARMBuildAttrs::AllowFPv3A,
878 /* OverwriteExisting= */ false);
879 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
880 ARMBuildAttrs::AllowNeon,
881 /* OverwriteExisting= */ false);
882 break;
883
Javed Absard5526302015-06-29 09:32:29 +0000884 case ARM::FK_NEON_FP16:
885 setAttributeItem(ARMBuildAttrs::FP_arch,
886 ARMBuildAttrs::AllowFPv3A,
887 /* OverwriteExisting= */ false);
888 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
889 ARMBuildAttrs::AllowNeon,
890 /* OverwriteExisting= */ false);
891 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
892 ARMBuildAttrs::AllowHPFP,
893 /* OverwriteExisting= */ false);
894 break;
895
Renato Golin35de35d2015-05-12 10:33:58 +0000896 case ARM::FK_NEON_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000897 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000898 ARMBuildAttrs::AllowFPv4A,
899 /* OverwriteExisting= */ false);
900 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
901 ARMBuildAttrs::AllowNeon2,
902 /* OverwriteExisting= */ false);
903 break;
904
Renato Golin35de35d2015-05-12 10:33:58 +0000905 case ARM::FK_NEON_FP_ARMV8:
906 case ARM::FK_CRYPTO_NEON_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000907 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000908 ARMBuildAttrs::AllowFPARMv8A,
909 /* OverwriteExisting= */ false);
Vladimir Sukharevc632cda2015-03-26 17:05:54 +0000910 // 'Advanced_SIMD_arch' must be emitted not here, but within
911 // ARMAsmPrinter::emitAttributes(), depending on hasV8Ops() and hasV8_1a()
Logan Chien8cbb80d2013-10-28 17:51:12 +0000912 break;
913
Renato Golin35de35d2015-05-12 10:33:58 +0000914 case ARM::FK_SOFTVFP:
John Brawn985c04e2015-06-05 13:31:19 +0000915 case ARM::FK_NONE:
Logan Chien05ae7442014-01-02 15:50:02 +0000916 break;
917
Logan Chien8cbb80d2013-10-28 17:51:12 +0000918 default:
919 report_fatal_error("Unknown FPU: " + Twine(FPU));
920 break;
921 }
922}
923size_t ARMTargetELFStreamer::calculateContentSize() const {
924 size_t Result = 0;
925 for (size_t i = 0; i < Contents.size(); ++i) {
926 AttributeItem item = Contents[i];
927 switch (item.Type) {
928 case AttributeItem::HiddenAttribute:
929 break;
930 case AttributeItem::NumericAttribute:
Logan Chien5b776b72014-02-22 14:00:39 +0000931 Result += getULEB128Size(item.Tag);
932 Result += getULEB128Size(item.IntValue);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000933 break;
934 case AttributeItem::TextAttribute:
Logan Chien5b776b72014-02-22 14:00:39 +0000935 Result += getULEB128Size(item.Tag);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000936 Result += item.StringValue.size() + 1; // string + '\0'
937 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000938 case AttributeItem::NumericAndTextAttributes:
Logan Chien5b776b72014-02-22 14:00:39 +0000939 Result += getULEB128Size(item.Tag);
940 Result += getULEB128Size(item.IntValue);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000941 Result += item.StringValue.size() + 1; // string + '\0';
942 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000943 }
944 }
945 return Result;
946}
947void ARMTargetELFStreamer::finishAttributeSection() {
948 // <format-version>
949 // [ <section-length> "vendor-name"
950 // [ <file-tag> <size> <attribute>*
951 // | <section-tag> <size> <section-number>* 0 <attribute>*
952 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
953 // ]+
954 // ]*
955
Renato Golin35de35d2015-05-12 10:33:58 +0000956 if (FPU != ARM::FK_INVALID)
Logan Chien8cbb80d2013-10-28 17:51:12 +0000957 emitFPUDefaultAttributes();
958
Renato Golin35de35d2015-05-12 10:33:58 +0000959 if (Arch != ARM::AK_INVALID)
Logan Chien439e8f92013-12-11 17:16:25 +0000960 emitArchDefaultAttributes();
961
Logan Chien8cbb80d2013-10-28 17:51:12 +0000962 if (Contents.empty())
963 return;
964
965 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
966
967 ARMELFStreamer &Streamer = getStreamer();
968
969 // Switch to .ARM.attributes section
970 if (AttributeSection) {
971 Streamer.SwitchSection(AttributeSection);
972 } else {
Rafael Espindolaba31e272015-01-29 17:33:21 +0000973 AttributeSection = Streamer.getContext().getELFSection(
974 ".ARM.attributes", ELF::SHT_ARM_ATTRIBUTES, 0);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000975 Streamer.SwitchSection(AttributeSection);
976
977 // Format version
978 Streamer.EmitIntValue(0x41, 1);
979 }
980
981 // Vendor size + Vendor name + '\0'
982 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
983
984 // Tag + Tag Size
985 const size_t TagHeaderSize = 1 + 4;
986
987 const size_t ContentsSize = calculateContentSize();
988
989 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
990 Streamer.EmitBytes(CurrentVendor);
991 Streamer.EmitIntValue(0, 1); // '\0'
992
993 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
994 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
995
996 // Size should have been accounted for already, now
997 // emit each field as its type (ULEB or String)
998 for (size_t i = 0; i < Contents.size(); ++i) {
999 AttributeItem item = Contents[i];
1000 Streamer.EmitULEB128IntValue(item.Tag);
1001 switch (item.Type) {
1002 default: llvm_unreachable("Invalid attribute type");
1003 case AttributeItem::NumericAttribute:
1004 Streamer.EmitULEB128IntValue(item.IntValue);
1005 break;
1006 case AttributeItem::TextAttribute:
Charlie Turner8d433692014-11-27 12:13:56 +00001007 Streamer.EmitBytes(item.StringValue);
Logan Chien8cbb80d2013-10-28 17:51:12 +00001008 Streamer.EmitIntValue(0, 1); // '\0'
1009 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +00001010 case AttributeItem::NumericAndTextAttributes:
1011 Streamer.EmitULEB128IntValue(item.IntValue);
Charlie Turner8d433692014-11-27 12:13:56 +00001012 Streamer.EmitBytes(item.StringValue);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +00001013 Streamer.EmitIntValue(0, 1); // '\0'
1014 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +00001015 }
1016 }
1017
1018 Contents.clear();
Renato Golin35de35d2015-05-12 10:33:58 +00001019 FPU = ARM::FK_INVALID;
Logan Chien8cbb80d2013-10-28 17:51:12 +00001020}
Rafael Espindola4c6f6132014-04-27 17:10:46 +00001021
1022void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
1023 ARMELFStreamer &Streamer = getStreamer();
1024 if (!Streamer.IsThumb)
1025 return;
1026
Rafael Espindolac73aed12015-06-03 19:03:11 +00001027 Streamer.getAssembler().registerSymbol(*Symbol);
Rafael Espindola95fb9b92015-06-02 20:38:46 +00001028 unsigned Type = cast<MCSymbolELF>(Symbol)->getType();
Rafael Espindolaf8794ff2015-06-04 00:47:43 +00001029 if (Type == ELF::STT_FUNC || Type == ELF::STT_GNU_IFUNC)
Rafael Espindola4c6f6132014-04-27 17:10:46 +00001030 Streamer.EmitThumbFunc(Symbol);
1031}
1032
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001033void
1034ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
1035 getStreamer().EmitFixup(S, FK_Data_4);
1036}
Rafael Espindola466d6632014-04-27 20:23:58 +00001037
1038void ARMTargetELFStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
1039 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
Rafael Espindola4a042942014-05-01 12:45:43 +00001040 const MCSymbol &Sym = SRE->getSymbol();
1041 if (!Sym.isDefined()) {
Rafael Espindola466d6632014-04-27 20:23:58 +00001042 getStreamer().EmitAssignment(Symbol, Value);
1043 return;
1044 }
1045 }
1046
1047 getStreamer().EmitThumbFunc(Symbol);
1048 getStreamer().EmitAssignment(Symbol, Value);
1049}
1050
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +00001051void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
1052 getStreamer().emitInst(Inst, Suffix);
1053}
Logan Chien8cbb80d2013-10-28 17:51:12 +00001054
Keno Fischer00021422016-01-12 13:38:15 +00001055void ARMTargetELFStreamer::reset() { AttributeSection = nullptr; }
1056
Logan Chien8cbb80d2013-10-28 17:51:12 +00001057void ARMELFStreamer::FinishImpl() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +00001058 MCTargetStreamer &TS = *getTargetStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +00001059 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1060 ATS.finishAttributeSection();
1061
1062 MCELFStreamer::FinishImpl();
1063}
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001064
Keno Fischer00021422016-01-12 13:38:15 +00001065void ARMELFStreamer::reset() {
1066 MCTargetStreamer &TS = *getTargetStreamer();
1067 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1068 ATS.reset();
1069 MappingSymbolCounter = 0;
1070 MCELFStreamer::reset();
1071 // MCELFStreamer clear's the assembler's e_flags. However, for
1072 // arm we manually set the ABI version on streamer creation, so
1073 // do the same here
1074 getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1075}
1076
Logan Chien2bcc42c2013-01-30 15:39:04 +00001077inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
1078 unsigned Type,
1079 unsigned Flags,
1080 SectionKind Kind,
1081 const MCSymbol &Fn) {
1082 const MCSectionELF &FnSection =
1083 static_cast<const MCSectionELF &>(Fn.getSection());
1084
1085 // Create the name for new section
1086 StringRef FnSecName(FnSection.getSectionName());
1087 SmallString<128> EHSecName(Prefix);
1088 if (FnSecName != ".text") {
1089 EHSecName += FnSecName;
1090 }
1091
1092 // Get .ARM.extab or .ARM.exidx section
Rafael Espindola0ccf9b72015-06-02 21:30:13 +00001093 const MCSymbolELF *Group = FnSection.getGroup();
Rafael Espindola61e8ce32015-04-06 04:25:18 +00001094 if (Group)
1095 Flags |= ELF::SHF_GROUP;
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001096 MCSectionELF *EHSection =
Rafael Espindola61e8ce32015-04-06 04:25:18 +00001097 getContext().getELFSection(EHSecName, Type, Flags, 0, Group,
1098 FnSection.getUniqueID(), nullptr, &FnSection);
1099
Logan Chiend8bb4b72013-04-16 12:02:21 +00001100 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001101
1102 // Switch to .ARM.extab or .ARM.exidx section
1103 SwitchSection(EHSection);
Rafael Espindola7b514962014-02-04 18:34:04 +00001104 EmitCodeAlignment(4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001105}
1106
1107inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
Rafael Espindola449711c2015-11-18 06:02:15 +00001108 SwitchToEHSection(".ARM.extab", ELF::SHT_PROGBITS, ELF::SHF_ALLOC,
1109 SectionKind::getData(), FnStart);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001110}
1111
1112inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
Rafael Espindola449711c2015-11-18 06:02:15 +00001113 SwitchToEHSection(".ARM.exidx", ELF::SHT_ARM_EXIDX,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001114 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
Rafael Espindola449711c2015-11-18 06:02:15 +00001115 SectionKind::getData(), FnStart);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001116}
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001117void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1118 MCDataFragment *Frag = getOrCreateDataFragment();
Jim Grosbach63661f82015-05-15 19:13:05 +00001119 Frag->getFixups().push_back(MCFixup::create(Frag->getContents().size(), Expr,
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001120 Kind));
1121}
Logan Chien2bcc42c2013-01-30 15:39:04 +00001122
Keno Fischer00021422016-01-12 13:38:15 +00001123void ARMELFStreamer::EHReset() {
Craig Topper062a2ba2014-04-25 05:30:21 +00001124 ExTab = nullptr;
1125 FnStart = nullptr;
1126 Personality = nullptr;
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001127 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
Logan Chien325823a2013-06-09 12:22:30 +00001128 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001129 FPOffset = 0;
1130 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +00001131 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001132 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001133 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001134
Logan Chien325823a2013-06-09 12:22:30 +00001135 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001136 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001137}
1138
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001139void ARMELFStreamer::emitFnStart() {
Craig Toppere73658d2014-04-28 04:05:08 +00001140 assert(FnStart == nullptr);
Jim Grosbach6f482002015-05-18 18:43:14 +00001141 FnStart = getContext().createTempSymbol();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001142 EmitLabel(FnStart);
1143}
1144
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001145void ARMELFStreamer::emitFnEnd() {
Alp Tokercb402912014-01-24 17:20:08 +00001146 assert(FnStart && ".fnstart must precedes .fnend");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001147
1148 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +00001149 if (!ExTab && !CantUnwind)
1150 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001151
1152 // Emit the exception index table entry
1153 SwitchToExIdxSection(*FnStart);
1154
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001155 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
Logan Chiend8bb4b72013-04-16 12:02:21 +00001156 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001157
1158 const MCSymbolRefExpr *FnStartRef =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001159 MCSymbolRefExpr::create(FnStart,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001160 MCSymbolRefExpr::VK_ARM_PREL31,
1161 getContext());
1162
Rafael Espindola64e1af82013-07-02 15:49:13 +00001163 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001164
1165 if (CantUnwind) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001166 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001167 } else if (ExTab) {
1168 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001169 const MCSymbolRefExpr *ExTabEntryRef =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001170 MCSymbolRefExpr::create(ExTab,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001171 MCSymbolRefExpr::VK_ARM_PREL31,
1172 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +00001173 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001174 } else {
1175 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1176 // the second word of exception index table entry. The size of the unwind
1177 // opcodes should always be 4 bytes.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001178 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
Jonathan Roelofs4971b402014-05-15 02:24:50 +00001179 "Compact model must use __aeabi_unwind_cpp_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +00001180 assert(Opcodes.size() == 4u &&
Jonathan Roelofs4971b402014-05-15 02:24:50 +00001181 "Unwind opcode size for __aeabi_unwind_cpp_pr0 must be equal to 4");
Christian Pirker39db7ec2014-05-13 16:44:30 +00001182 uint64_t Intval = Opcodes[0] |
1183 Opcodes[1] << 8 |
1184 Opcodes[2] << 16 |
1185 Opcodes[3] << 24;
1186 EmitIntValue(Intval, Opcodes.size());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001187 }
1188
Logan Chien4ea23b52013-05-10 16:17:24 +00001189 // Switch to the section containing FnStart
1190 SwitchSection(&FnStart->getSection());
1191
Logan Chien2bcc42c2013-01-30 15:39:04 +00001192 // Clean exception handling frame information
Keno Fischer00021422016-01-12 13:38:15 +00001193 EHReset();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001194}
1195
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001196void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1197
1198// Add the R_ARM_NONE fixup at the same position
1199void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
Jim Grosbach6f482002015-05-18 18:43:14 +00001200 const MCSymbol *PersonalitySym = getContext().getOrCreateSymbol(Name);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001201
Jim Grosbach13760bd2015-05-30 01:25:56 +00001202 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::create(
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001203 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1204
Rafael Espindola2be12812014-06-25 15:29:54 +00001205 visitUsedExpr(*PersonalityRef);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001206 MCDataFragment *DF = getOrCreateDataFragment();
Jim Grosbach63661f82015-05-15 19:13:05 +00001207 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001208 PersonalityRef,
1209 MCFixup::getKindForSize(4, false)));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001210}
1211
Logan Chien325823a2013-06-09 12:22:30 +00001212void ARMELFStreamer::FlushPendingOffset() {
1213 if (PendingOffset != 0) {
1214 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1215 PendingOffset = 0;
1216 }
1217}
1218
Logan Chienc931fce2013-07-02 12:43:27 +00001219void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +00001220 // Emit the unwind opcode to restore $sp.
1221 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001222 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +00001223 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1224 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001225 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +00001226 } else {
1227 FlushPendingOffset();
1228 }
1229
1230 // Finalize the unwind opcode sequence
1231 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +00001232
1233 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1234 // section. Thus, we don't have to create an entry in the .ARM.extab
1235 // section.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001236 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +00001237 return;
1238
1239 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001240 SwitchToExTabSection(*FnStart);
1241
1242 // Create .ARM.extab label for offset in .ARM.exidx
1243 assert(!ExTab);
Jim Grosbach6f482002015-05-18 18:43:14 +00001244 ExTab = getContext().createTempSymbol();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001245 EmitLabel(ExTab);
1246
Logan Chien4ea23b52013-05-10 16:17:24 +00001247 // Emit personality
1248 if (Personality) {
1249 const MCSymbolRefExpr *PersonalityRef =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001250 MCSymbolRefExpr::create(Personality,
Logan Chien4ea23b52013-05-10 16:17:24 +00001251 MCSymbolRefExpr::VK_ARM_PREL31,
1252 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001253
Rafael Espindola64e1af82013-07-02 15:49:13 +00001254 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +00001255 }
Logan Chien2bcc42c2013-01-30 15:39:04 +00001256
1257 // Emit unwind opcodes
Christian Pirker39db7ec2014-05-13 16:44:30 +00001258 assert((Opcodes.size() % 4) == 0 &&
1259 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be multiple of 4");
1260 for (unsigned I = 0; I != Opcodes.size(); I += 4) {
1261 uint64_t Intval = Opcodes[I] |
1262 Opcodes[I + 1] << 8 |
1263 Opcodes[I + 2] << 16 |
1264 Opcodes[I + 3] << 24;
1265 EmitIntValue(Intval, 4);
1266 }
Logan Chienc931fce2013-07-02 12:43:27 +00001267
1268 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1269 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1270 // after the unwind opcodes. The handler data consists of several 32-bit
1271 // words, and should be terminated by zero.
1272 //
1273 // In case that the .handlerdata directive is not specified by the
1274 // programmer, we should emit zero to terminate the handler data.
1275 if (NoHandlerData && !Personality)
1276 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001277}
1278
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001279void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
Logan Chien4ea23b52013-05-10 16:17:24 +00001280
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001281void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001282 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001283 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001284}
1285
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +00001286void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1287 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1288 PersonalityIndex = Index;
1289}
1290
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001291void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001292 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001293 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001294 "the operand of .setfp directive should be either $sp or $fp");
1295
1296 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +00001297 FPReg = NewFPReg;
1298
1299 if (NewSPReg == ARM::SP)
1300 FPOffset = SPOffset + Offset;
1301 else
1302 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001303}
1304
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +00001305void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
1306 assert((Reg != ARM::SP && Reg != ARM::PC) &&
1307 "the operand of .movsp cannot be either sp or pc");
1308 assert(FPReg == ARM::SP && "current FP must be SP");
1309
1310 FlushPendingOffset();
1311
1312 FPReg = Reg;
1313 FPOffset = SPOffset + Offset;
1314
1315 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1316 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1317}
1318
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001319void ARMELFStreamer::emitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001320 // Track the change of the $sp offset
1321 SPOffset -= Offset;
1322
1323 // To squash multiple .pad directives, we should delay the unwind opcode
1324 // until the .save, .vsave, .handlerdata, or .fnend directives.
1325 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001326}
1327
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001328void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001329 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +00001330 // Collect the registers in the register list
1331 unsigned Count = 0;
1332 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +00001333 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001334 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001335 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +00001336 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +00001337 unsigned Bit = (1u << Reg);
1338 if ((Mask & Bit) == 0) {
1339 Mask |= Bit;
1340 ++Count;
1341 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001342 }
Logan Chien325823a2013-06-09 12:22:30 +00001343
1344 // Track the change the $sp offset: For the .save directive, the
1345 // corresponding push instruction will decrease the $sp by (4 * Count).
1346 // For the .vsave directive, the corresponding vpush instruction will
1347 // decrease $sp by (8 * Count).
1348 SPOffset -= Count * (IsVector ? 8 : 4);
1349
1350 // Emit the opcode
1351 FlushPendingOffset();
1352 if (IsVector)
1353 UnwindOpAsm.EmitVFPRegSave(Mask);
1354 else
1355 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001356}
1357
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +00001358void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1359 const SmallVectorImpl<uint8_t> &Opcodes) {
1360 FlushPendingOffset();
1361 SPOffset = SPOffset - Offset;
1362 UnwindOpAsm.EmitRaw(Opcodes);
1363}
1364
Tim Northover5cc3dc82012-12-07 16:50:23 +00001365namespace llvm {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001366
Rafael Espindola73870dd2015-03-16 21:43:42 +00001367MCTargetStreamer *createARMTargetAsmStreamer(MCStreamer &S,
1368 formatted_raw_ostream &OS,
1369 MCInstPrinter *InstPrint,
1370 bool isVerboseAsm) {
1371 return new ARMTargetAsmStreamer(S, OS, *InstPrint, isVerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001372}
1373
Peter Collingbourne20c72592015-02-19 00:45:02 +00001374MCTargetStreamer *createARMNullTargetStreamer(MCStreamer &S) {
1375 return new ARMTargetStreamer(S);
1376}
1377
Rafael Espindolacd584a82015-03-19 01:50:16 +00001378MCTargetStreamer *createARMObjectTargetStreamer(MCStreamer &S,
1379 const MCSubtargetInfo &STI) {
Daniel Sanders50f17232015-09-15 16:17:27 +00001380 const Triple &TT = STI.getTargetTriple();
Daniel Sandersfbdab432015-07-06 16:33:18 +00001381 if (TT.isOSBinFormatELF())
Rafael Espindolacd584a82015-03-19 01:50:16 +00001382 return new ARMTargetELFStreamer(S);
1383 return new ARMTargetStreamer(S);
1384}
1385
Rafael Espindola7b61ddf2014-10-15 16:12:52 +00001386MCELFStreamer *createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001387 raw_pwrite_stream &OS,
1388 MCCodeEmitter *Emitter, bool RelaxAll,
1389 bool IsThumb) {
Rafael Espindola24ea09e2014-01-26 06:06:37 +00001390 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001391 // FIXME: This should eventually end up somewhere else where more
1392 // intelligent flag decisions can be made. For now we are just maintaining
1393 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1394 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1395
Tim Northover5cc3dc82012-12-07 16:50:23 +00001396 if (RelaxAll)
1397 S->getAssembler().setRelaxAll(true);
Tim Northover5cc3dc82012-12-07 16:50:23 +00001398 return S;
1399 }
1400
Alexander Kornienkof00654e2015-06-23 09:49:53 +00001401}
Tim Northover5cc3dc82012-12-07 16:50:23 +00001402
1403