blob: 7e56ba39eef0c49b0ce97a38530a4cfb367a9d9c [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"
Eugene Zelenkoe79c0772017-01-27 23:58:02 +000018#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/SmallString.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/ADT/Triple.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000023#include "llvm/ADT/Twine.h"
24#include "llvm/MC/MCAsmBackend.h"
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +000025#include "llvm/MC/MCAsmInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000026#include "llvm/MC/MCAssembler.h"
27#include "llvm/MC/MCCodeEmitter.h"
28#include "llvm/MC/MCContext.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000029#include "llvm/MC/MCELFStreamer.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000030#include "llvm/MC/MCExpr.h"
Eugene Zelenkoe79c0772017-01-27 23:58:02 +000031#include "llvm/MC/MCFixup.h"
32#include "llvm/MC/MCFragment.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000033#include "llvm/MC/MCInst.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000034#include "llvm/MC/MCInstPrinter.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000035#include "llvm/MC/MCRegisterInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000036#include "llvm/MC/MCSection.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000037#include "llvm/MC/MCSectionELF.h"
38#include "llvm/MC/MCStreamer.h"
Eugene Zelenkoe79c0772017-01-27 23:58:02 +000039#include "llvm/MC/MCSubtargetInfo.h"
40#include "llvm/MC/MCSymbol.h"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000041#include "llvm/MC/MCSymbolELF.h"
Eugene Zelenkoe79c0772017-01-27 23:58:02 +000042#include "llvm/MC/SectionKind.h"
Saleem Abdulrasool278a9f42014-01-19 08:25:27 +000043#include "llvm/Support/ARMBuildAttributes.h"
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +000044#include "llvm/Support/ARMEHABI.h"
Eugene Zelenkoe79c0772017-01-27 23:58:02 +000045#include "llvm/Support/Casting.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000046#include "llvm/Support/ELF.h"
Eugene Zelenkoe79c0772017-01-27 23:58:02 +000047#include "llvm/Support/ErrorHandling.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000048#include "llvm/Support/FormattedStream.h"
Logan Chien5b776b72014-02-22 14:00:39 +000049#include "llvm/Support/LEB128.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000050#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe79c0772017-01-27 23:58:02 +000051#include "llvm/Support/TargetParser.h"
Logan Chien8cbb80d2013-10-28 17:51:12 +000052#include <algorithm>
Eugene Zelenkoe79c0772017-01-27 23:58:02 +000053#include <cassert>
54#include <climits>
55#include <cstddef>
56#include <cstdint>
57#include <string>
Tim Northover5cc3dc82012-12-07 16:50:23 +000058
59using namespace llvm;
60
Logan Chiend8bb4b72013-04-16 12:02:21 +000061static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +000062 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX &&
63 "Invalid personality index");
Logan Chiend8bb4b72013-04-16 12:02:21 +000064 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
65}
66
Tim Northover5cc3dc82012-12-07 16:50:23 +000067namespace {
68
Rafael Espindolaa17151a2013-10-08 13:08:17 +000069class ARMELFStreamer;
70
71class ARMTargetAsmStreamer : public ARMTargetStreamer {
72 formatted_raw_ostream &OS;
73 MCInstPrinter &InstPrinter;
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +000074 bool IsVerboseAsm;
Rafael Espindolaa17151a2013-10-08 13:08:17 +000075
Craig Topperca7e3e52014-03-10 03:19:03 +000076 void emitFnStart() override;
77 void emitFnEnd() override;
78 void emitCantUnwind() override;
79 void emitPersonality(const MCSymbol *Personality) override;
80 void emitPersonalityIndex(unsigned Index) override;
81 void emitHandlerData() override;
82 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
83 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
84 void emitPad(int64_t Offset) override;
85 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
86 bool isVector) override;
87 void emitUnwindRaw(int64_t Offset,
88 const SmallVectorImpl<uint8_t> &Opcodes) override;
Rafael Espindolaa17151a2013-10-08 13:08:17 +000089
Craig Topperca7e3e52014-03-10 03:19:03 +000090 void switchVendor(StringRef Vendor) override;
91 void emitAttribute(unsigned Attribute, unsigned Value) override;
92 void emitTextAttribute(unsigned Attribute, StringRef String) override;
93 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
Artyom Skrobove9b3fb82015-12-07 14:22:39 +000094 StringRef StringValue) override;
Craig Topperca7e3e52014-03-10 03:19:03 +000095 void emitArch(unsigned Arch) override;
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +000096 void emitArchExtension(unsigned ArchExt) override;
Craig Topperca7e3e52014-03-10 03:19:03 +000097 void emitObjectArch(unsigned Arch) override;
98 void emitFPU(unsigned FPU) override;
99 void emitInst(uint32_t Inst, char Suffix = '\0') override;
100 void finishAttributeSection() override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000101
Craig Topperca7e3e52014-03-10 03:19:03 +0000102 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
Rafael Espindola466d6632014-04-27 20:23:58 +0000103 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000104
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000105public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000106 ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
107 MCInstPrinter &InstPrinter, bool VerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000108};
109
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000110ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
111 formatted_raw_ostream &OS,
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000112 MCInstPrinter &InstPrinter,
113 bool VerboseAsm)
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000114 : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
115 IsVerboseAsm(VerboseAsm) {}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000116
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000117void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
118void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
119void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000120
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000121void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
122 OS << "\t.personality " << Personality->getName() << '\n';
123}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000124
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000125void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
126 OS << "\t.personalityindex " << Index << '\n';
127}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000128
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000129void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000130
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000131void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
132 int64_t Offset) {
133 OS << "\t.setfp\t";
134 InstPrinter.printRegName(OS, FpReg);
135 OS << ", ";
136 InstPrinter.printRegName(OS, SpReg);
137 if (Offset)
138 OS << ", #" << Offset;
139 OS << '\n';
140}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000141
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000142void ARMTargetAsmStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
143 assert((Reg != ARM::SP && Reg != ARM::PC) &&
144 "the operand of .movsp cannot be either sp or pc");
145
146 OS << "\t.movsp\t";
147 InstPrinter.printRegName(OS, Reg);
148 if (Offset)
149 OS << ", #" << Offset;
150 OS << '\n';
151}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000152
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000153void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
154 OS << "\t.pad\t#" << Offset << '\n';
155}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000156
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000157void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
158 bool isVector) {
159 assert(RegList.size() && "RegList should not be empty");
160 if (isVector)
161 OS << "\t.vsave\t{";
162 else
163 OS << "\t.save\t{";
164
165 InstPrinter.printRegName(OS, RegList[0]);
166
167 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
168 OS << ", ";
169 InstPrinter.printRegName(OS, RegList[i]);
170 }
171
172 OS << "}\n";
173}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000174
175void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {}
176
Logan Chien8cbb80d2013-10-28 17:51:12 +0000177void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000178 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
179 if (IsVerboseAsm) {
180 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
181 if (!Name.empty())
182 OS << "\t@ " << Name;
183 }
184 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000185}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000186
Logan Chien8cbb80d2013-10-28 17:51:12 +0000187void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
188 StringRef String) {
189 switch (Attribute) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000190 case ARMBuildAttrs::CPU_name:
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000191 OS << "\t.cpu\t" << String.lower();
192 break;
193 default:
194 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000195 if (IsVerboseAsm) {
196 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
197 if (!Name.empty())
198 OS << "\t@ " << Name;
199 }
Logan Chien8cbb80d2013-10-28 17:51:12 +0000200 break;
201 }
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000202 OS << "\n";
203}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000204
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000205void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
206 unsigned IntValue,
207 StringRef StringValue) {
208 switch (Attribute) {
209 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
210 case ARMBuildAttrs::compatibility:
211 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
212 if (!StringValue.empty())
213 OS << ", \"" << StringValue << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000214 if (IsVerboseAsm)
215 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000216 break;
217 }
218 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000219}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000220
Logan Chien439e8f92013-12-11 17:16:25 +0000221void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000222 OS << "\t.arch\t" << ARM::getArchName(Arch) << "\n";
Logan Chien439e8f92013-12-11 17:16:25 +0000223}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000224
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +0000225void ARMTargetAsmStreamer::emitArchExtension(unsigned ArchExt) {
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000226 OS << "\t.arch_extension\t" << ARM::getArchExtName(ArchExt) << "\n";
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +0000227}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000228
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000229void ARMTargetAsmStreamer::emitObjectArch(unsigned Arch) {
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000230 OS << "\t.object_arch\t" << ARM::getArchName(Arch) << '\n';
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000231}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000232
Logan Chien8cbb80d2013-10-28 17:51:12 +0000233void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000234 OS << "\t.fpu\t" << ARM::getFPUName(FPU) << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000235}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000236
237void ARMTargetAsmStreamer::finishAttributeSection() {}
238
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000239void
240ARMTargetAsmStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
241 OS << "\t.tlsdescseq\t" << S->getSymbol().getName();
242}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000243
Rafael Espindola466d6632014-04-27 20:23:58 +0000244void ARMTargetAsmStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
Matt Arsenault8b643552015-06-09 00:31:39 +0000245 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
246
247 OS << "\t.thumb_set\t";
248 Symbol->print(OS, MAI);
249 OS << ", ";
250 Value->print(OS, MAI);
251 OS << '\n';
Rafael Espindola466d6632014-04-27 20:23:58 +0000252}
253
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000254void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
255 OS << "\t.inst";
256 if (Suffix)
257 OS << "." << Suffix;
Benjamin Kramerbe48c402015-05-23 16:39:10 +0000258 OS << "\t0x" << Twine::utohexstr(Inst) << "\n";
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000259}
260
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000261void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
262 const SmallVectorImpl<uint8_t> &Opcodes) {
263 OS << "\t.unwind_raw " << Offset;
264 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
265 OCE = Opcodes.end();
266 OCI != OCE; ++OCI)
Benjamin Kramerbe48c402015-05-23 16:39:10 +0000267 OS << ", 0x" << Twine::utohexstr(*OCI);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000268 OS << '\n';
269}
270
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000271class ARMTargetELFStreamer : public ARMTargetStreamer {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000272private:
273 // This structure holds all attributes, accounting for
Artyom Skrobove9b3fb82015-12-07 14:22:39 +0000274 // their string/numeric value, so we can later emit them
Logan Chien8cbb80d2013-10-28 17:51:12 +0000275 // in declaration order, keeping all in the same vector
276 struct AttributeItem {
277 enum {
278 HiddenAttribute = 0,
279 NumericAttribute,
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000280 TextAttribute,
281 NumericAndTextAttributes
Logan Chien8cbb80d2013-10-28 17:51:12 +0000282 } Type;
283 unsigned Tag;
284 unsigned IntValue;
Artyom Skrobove9b3fb82015-12-07 14:22:39 +0000285 std::string StringValue;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000286
287 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
Charlie Turner8b2caa42015-01-05 13:12:17 +0000288 // The conformance tag must be emitted first when serialised
289 // into an object file. Specifically, the addenda to the ARM ABI
290 // states that (2.3.7.4):
291 //
292 // "To simplify recognition by consumers in the common case of
293 // claiming conformity for the whole file, this tag should be
294 // emitted first in a file-scope sub-subsection of the first
295 // public subsection of the attributes section."
296 //
297 // So it is special-cased in this comparison predicate when the
298 // attributes are sorted in finishAttributeSection().
299 return (RHS.Tag != ARMBuildAttrs::conformance) &&
300 ((LHS.Tag == ARMBuildAttrs::conformance) || (LHS.Tag < RHS.Tag));
Logan Chien8cbb80d2013-10-28 17:51:12 +0000301 }
302 };
303
304 StringRef CurrentVendor;
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000305 unsigned FPU = ARM::FK_INVALID;
306 unsigned Arch = ARM::AK_INVALID;
307 unsigned EmittedArch = ARM::AK_INVALID;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000308 SmallVector<AttributeItem, 64> Contents;
309
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000310 MCSection *AttributeSection = nullptr;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000311
Logan Chien8cbb80d2013-10-28 17:51:12 +0000312 AttributeItem *getAttributeItem(unsigned Attribute) {
313 for (size_t i = 0; i < Contents.size(); ++i)
314 if (Contents[i].Tag == Attribute)
315 return &Contents[i];
Craig Topper062a2ba2014-04-25 05:30:21 +0000316 return nullptr;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000317 }
318
319 void setAttributeItem(unsigned Attribute, unsigned Value,
320 bool OverwriteExisting) {
321 // Look for existing attribute item
322 if (AttributeItem *Item = getAttributeItem(Attribute)) {
323 if (!OverwriteExisting)
324 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000325 Item->Type = AttributeItem::NumericAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000326 Item->IntValue = Value;
327 return;
328 }
329
330 // Create new attribute item
331 AttributeItem Item = {
332 AttributeItem::NumericAttribute,
333 Attribute,
334 Value,
335 StringRef("")
336 };
337 Contents.push_back(Item);
338 }
339
340 void setAttributeItem(unsigned Attribute, StringRef Value,
341 bool OverwriteExisting) {
342 // Look for existing attribute item
343 if (AttributeItem *Item = getAttributeItem(Attribute)) {
344 if (!OverwriteExisting)
345 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000346 Item->Type = AttributeItem::TextAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000347 Item->StringValue = Value;
348 return;
349 }
350
351 // Create new attribute item
352 AttributeItem Item = {
353 AttributeItem::TextAttribute,
354 Attribute,
355 0,
356 Value
357 };
358 Contents.push_back(Item);
359 }
360
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000361 void setAttributeItems(unsigned Attribute, unsigned IntValue,
362 StringRef StringValue, bool OverwriteExisting) {
363 // Look for existing attribute item
364 if (AttributeItem *Item = getAttributeItem(Attribute)) {
365 if (!OverwriteExisting)
366 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000367 Item->Type = AttributeItem::NumericAndTextAttributes;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000368 Item->IntValue = IntValue;
369 Item->StringValue = StringValue;
370 return;
371 }
372
373 // Create new attribute item
374 AttributeItem Item = {
375 AttributeItem::NumericAndTextAttributes,
376 Attribute,
377 IntValue,
378 StringValue
379 };
380 Contents.push_back(Item);
381 }
382
Logan Chien439e8f92013-12-11 17:16:25 +0000383 void emitArchDefaultAttributes();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000384 void emitFPUDefaultAttributes();
385
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000386 ARMELFStreamer &getStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000387
Craig Topperca7e3e52014-03-10 03:19:03 +0000388 void emitFnStart() override;
389 void emitFnEnd() override;
390 void emitCantUnwind() override;
391 void emitPersonality(const MCSymbol *Personality) override;
392 void emitPersonalityIndex(unsigned Index) override;
393 void emitHandlerData() override;
394 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
395 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
396 void emitPad(int64_t Offset) override;
397 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
398 bool isVector) override;
399 void emitUnwindRaw(int64_t Offset,
400 const SmallVectorImpl<uint8_t> &Opcodes) override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000401
Craig Topperca7e3e52014-03-10 03:19:03 +0000402 void switchVendor(StringRef Vendor) override;
403 void emitAttribute(unsigned Attribute, unsigned Value) override;
404 void emitTextAttribute(unsigned Attribute, StringRef String) override;
405 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
406 StringRef StringValue) override;
407 void emitArch(unsigned Arch) override;
408 void emitObjectArch(unsigned Arch) override;
409 void emitFPU(unsigned FPU) override;
410 void emitInst(uint32_t Inst, char Suffix = '\0') override;
411 void finishAttributeSection() override;
Rafael Espindola4c6f6132014-04-27 17:10:46 +0000412 void emitLabel(MCSymbol *Symbol) override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000413
Craig Topperca7e3e52014-03-10 03:19:03 +0000414 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
Rafael Espindola466d6632014-04-27 20:23:58 +0000415 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000416
Logan Chien8cbb80d2013-10-28 17:51:12 +0000417 size_t calculateContentSize() const;
418
Keno Fischer00021422016-01-12 13:38:15 +0000419 // Reset state between object emissions
420 void reset() override;
421
Logan Chien8cbb80d2013-10-28 17:51:12 +0000422public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000423 ARMTargetELFStreamer(MCStreamer &S)
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000424 : ARMTargetStreamer(S), CurrentVendor("aeabi") {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000425};
426
Tim Northover5cc3dc82012-12-07 16:50:23 +0000427/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
428/// the appropriate points in the object files. These symbols are defined in the
429/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
430///
431/// In brief: $a, $t or $d should be emitted at the start of each contiguous
432/// region of ARM code, Thumb code or data in a section. In practice, this
433/// emission does not rely on explicit assembler directives but on inherent
434/// properties of the directives doing the emission (e.g. ".byte" is data, "add
435/// r0, r0, r0" an instruction).
436///
437/// As a result this system is orthogonal to the DataRegion infrastructure used
438/// by MachO. Beware!
439class ARMELFStreamer : public MCELFStreamer {
440public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000441 friend class ARMTargetELFStreamer;
442
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000443 ARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_pwrite_stream &OS,
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000444 MCCodeEmitter *Emitter, bool IsThumb)
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000445 : MCELFStreamer(Context, TAB, OS, Emitter), IsThumb(IsThumb) {
Keno Fischer00021422016-01-12 13:38:15 +0000446 EHReset();
Logan Chiend8bb4b72013-04-16 12:02:21 +0000447 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000448
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000449 ~ARMELFStreamer() override = default;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000450
Craig Topperca7e3e52014-03-10 03:19:03 +0000451 void FinishImpl() override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000452
Logan Chien2bcc42c2013-01-30 15:39:04 +0000453 // ARM exception handling directives
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000454 void emitFnStart();
455 void emitFnEnd();
456 void emitCantUnwind();
457 void emitPersonality(const MCSymbol *Per);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000458 void emitPersonalityIndex(unsigned index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000459 void emitHandlerData();
460 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000461 void emitMovSP(unsigned Reg, int64_t Offset = 0);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000462 void emitPad(int64_t Offset);
463 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000464 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000465
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000466 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000467 // We have to keep track of the mapping symbol state of any sections we
468 // use. Each one should start off as EMS_None, which is provided as the
469 // default constructor by DenseMap::lookup.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000470 LastMappingSymbols[getPreviousSection().first] = LastEMS;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000471 LastEMS = LastMappingSymbols.lookup(Section);
472
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000473 MCELFStreamer::ChangeSection(Section, Subsection);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000474 }
475
476 /// This function is the one used to emit instruction data into the ELF
477 /// streamer. We override it to add the appropriate mapping symbol if
478 /// necessary.
Craig Topperca7e3e52014-03-10 03:19:03 +0000479 void EmitInstruction(const MCInst& Inst,
480 const MCSubtargetInfo &STI) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000481 if (IsThumb)
482 EmitThumbMappingSymbol();
483 else
484 EmitARMMappingSymbol();
485
David Woodhousee6c13e42014-01-28 23:12:42 +0000486 MCELFStreamer::EmitInstruction(Inst, STI);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000487 }
488
Craig Topperd25ff6f2014-03-10 03:22:59 +0000489 void emitInst(uint32_t Inst, char Suffix) {
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000490 unsigned Size;
491 char Buffer[4];
492 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
493
494 switch (Suffix) {
495 case '\0':
496 Size = 4;
497
498 assert(!IsThumb);
499 EmitARMMappingSymbol();
500 for (unsigned II = 0, IE = Size; II != IE; II++) {
501 const unsigned I = LittleEndian ? (Size - II - 1) : II;
502 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
503 }
504
505 break;
506 case 'n':
507 case 'w':
508 Size = (Suffix == 'n' ? 2 : 4);
509
510 assert(IsThumb);
511 EmitThumbMappingSymbol();
512 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
513 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
514 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
515 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
516 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
517 }
518
519 break;
520 default:
521 llvm_unreachable("Invalid Suffix");
522 }
523
524 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
525 }
526
Tim Northover5cc3dc82012-12-07 16:50:23 +0000527 /// This is one of the functions used to emit data into an ELF section, so the
528 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
529 /// necessary.
Craig Topperca7e3e52014-03-10 03:19:03 +0000530 void EmitBytes(StringRef Data) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000531 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000532 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000533 }
534
535 /// This is one of the functions used to emit data into an ELF section, so the
536 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
537 /// necessary.
Craig Topper3c76c522015-09-20 23:35:59 +0000538 void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
Saleem Abdulrasoolfe781972015-01-11 04:39:18 +0000539 if (const MCSymbolRefExpr *SRE = dyn_cast_or_null<MCSymbolRefExpr>(Value))
Oliver Stannard9be59af2015-11-17 10:00:43 +0000540 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_SBREL && !(Size == 4)) {
541 getContext().reportError(Loc, "relocated expression must be 32-bit");
542 return;
543 }
Saleem Abdulrasoolfe781972015-01-11 04:39:18 +0000544
Tim Northover5cc3dc82012-12-07 16:50:23 +0000545 EmitDataMappingSymbol();
Oliver Stannard09be0602015-11-16 16:22:47 +0000546 MCELFStreamer::EmitValueImpl(Value, Size, Loc);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000547 }
548
Craig Topperca7e3e52014-03-10 03:19:03 +0000549 void EmitAssemblerFlag(MCAssemblerFlag Flag) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000550 MCELFStreamer::EmitAssemblerFlag(Flag);
551
552 switch (Flag) {
553 case MCAF_SyntaxUnified:
554 return; // no-op here.
555 case MCAF_Code16:
556 IsThumb = true;
557 return; // Change to Thumb mode
558 case MCAF_Code32:
559 IsThumb = false;
560 return; // Change to ARM mode
561 case MCAF_Code64:
562 return;
563 case MCAF_SubsectionsViaSymbols:
564 return;
565 }
566 }
567
568private:
569 enum ElfMappingSymbol {
570 EMS_None,
571 EMS_ARM,
572 EMS_Thumb,
573 EMS_Data
574 };
575
576 void EmitDataMappingSymbol() {
577 if (LastEMS == EMS_Data) return;
578 EmitMappingSymbol("$d");
579 LastEMS = EMS_Data;
580 }
581
582 void EmitThumbMappingSymbol() {
583 if (LastEMS == EMS_Thumb) return;
584 EmitMappingSymbol("$t");
585 LastEMS = EMS_Thumb;
586 }
587
588 void EmitARMMappingSymbol() {
589 if (LastEMS == EMS_ARM) return;
590 EmitMappingSymbol("$a");
591 LastEMS = EMS_ARM;
592 }
593
594 void EmitMappingSymbol(StringRef Name) {
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000595 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
596 Name + "." + Twine(MappingSymbolCounter++)));
Rafael Espindolac5fb5082015-06-26 11:31:13 +0000597 EmitLabel(Symbol);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000598
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000599 Symbol->setType(ELF::STT_NOTYPE);
600 Symbol->setBinding(ELF::STB_LOCAL);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000601 Symbol->setExternal(false);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000602 }
603
Craig Topperca7e3e52014-03-10 03:19:03 +0000604 void EmitThumbFunc(MCSymbol *Func) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000605 getAssembler().setIsThumbFunc(Func);
Rafael Espindolab60c8292014-04-29 12:46:50 +0000606 EmitSymbolAttribute(Func, MCSA_ELF_TypeFunction);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000607 }
608
Logan Chien2bcc42c2013-01-30 15:39:04 +0000609 // Helper functions for ARM exception handling directives
Keno Fischer00021422016-01-12 13:38:15 +0000610 void EHReset();
611
612 // Reset state between object emissions
613 void reset() override;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000614
615 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000616 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000617 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000618
Mehdi Amini68c6c8c2016-10-07 08:48:07 +0000619 void SwitchToEHSection(StringRef Prefix, unsigned Type, unsigned Flags,
Logan Chien2bcc42c2013-01-30 15:39:04 +0000620 SectionKind Kind, const MCSymbol &Fn);
621 void SwitchToExTabSection(const MCSymbol &FnStart);
622 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000623
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000624 void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
625
Tim Northover5cc3dc82012-12-07 16:50:23 +0000626 bool IsThumb;
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000627 int64_t MappingSymbolCounter = 0;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000628
629 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000630 ElfMappingSymbol LastEMS = EMS_None;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000631
Logan Chien2bcc42c2013-01-30 15:39:04 +0000632 // ARM Exception Handling Frame Information
633 MCSymbol *ExTab;
634 MCSymbol *FnStart;
635 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000636 unsigned PersonalityIndex;
637 unsigned FPReg; // Frame pointer register
638 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
639 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
640 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000641 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000642 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000643 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000644 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000645};
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000646
Logan Chiend8bb4b72013-04-16 12:02:21 +0000647} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000648
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000649ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000650 return static_cast<ARMELFStreamer &>(Streamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000651}
652
653void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
654void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
655void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000656
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000657void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
658 getStreamer().emitPersonality(Personality);
659}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000660
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000661void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
662 getStreamer().emitPersonalityIndex(Index);
663}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000664
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000665void ARMTargetELFStreamer::emitHandlerData() {
666 getStreamer().emitHandlerData();
667}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000668
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000669void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
670 int64_t Offset) {
671 getStreamer().emitSetFP(FpReg, SpReg, Offset);
672}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000673
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000674void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
675 getStreamer().emitMovSP(Reg, Offset);
676}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000677
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000678void ARMTargetELFStreamer::emitPad(int64_t Offset) {
679 getStreamer().emitPad(Offset);
680}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000681
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000682void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
683 bool isVector) {
684 getStreamer().emitRegSave(RegList, isVector);
685}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000686
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000687void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
688 const SmallVectorImpl<uint8_t> &Opcodes) {
689 getStreamer().emitUnwindRaw(Offset, Opcodes);
690}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000691
Logan Chien8cbb80d2013-10-28 17:51:12 +0000692void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
693 assert(!Vendor.empty() && "Vendor cannot be empty.");
694
695 if (CurrentVendor == Vendor)
696 return;
697
698 if (!CurrentVendor.empty())
699 finishAttributeSection();
700
701 assert(Contents.empty() &&
702 ".ARM.attributes should be flushed before changing vendor");
703 CurrentVendor = Vendor;
704
705}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000706
Logan Chien8cbb80d2013-10-28 17:51:12 +0000707void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
708 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
709}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000710
Logan Chien8cbb80d2013-10-28 17:51:12 +0000711void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
712 StringRef Value) {
713 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
714}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000715
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000716void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
717 unsigned IntValue,
718 StringRef StringValue) {
719 setAttributeItems(Attribute, IntValue, StringValue,
720 /* OverwriteExisting= */ true);
721}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000722
Logan Chien439e8f92013-12-11 17:16:25 +0000723void ARMTargetELFStreamer::emitArch(unsigned Value) {
724 Arch = Value;
725}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000726
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000727void ARMTargetELFStreamer::emitObjectArch(unsigned Value) {
728 EmittedArch = Value;
729}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000730
Logan Chien439e8f92013-12-11 17:16:25 +0000731void ARMTargetELFStreamer::emitArchDefaultAttributes() {
732 using namespace ARMBuildAttrs;
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000733
Renato Golinf5f373f2015-05-08 21:04:27 +0000734 setAttributeItem(CPU_name,
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000735 ARM::getCPUAttr(Arch),
Renato Golinf5f373f2015-05-08 21:04:27 +0000736 false);
737
Renato Golin35de35d2015-05-12 10:33:58 +0000738 if (EmittedArch == ARM::AK_INVALID)
Renato Golinf5f373f2015-05-08 21:04:27 +0000739 setAttributeItem(CPU_arch,
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000740 ARM::getArchAttr(Arch),
Renato Golinf5f373f2015-05-08 21:04:27 +0000741 false);
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000742 else
Renato Golinf5f373f2015-05-08 21:04:27 +0000743 setAttributeItem(CPU_arch,
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000744 ARM::getArchAttr(EmittedArch),
Renato Golinf5f373f2015-05-08 21:04:27 +0000745 false);
Logan Chien439e8f92013-12-11 17:16:25 +0000746
747 switch (Arch) {
Renato Golin35de35d2015-05-12 10:33:58 +0000748 case ARM::AK_ARMV2:
749 case ARM::AK_ARMV2A:
750 case ARM::AK_ARMV3:
751 case ARM::AK_ARMV3M:
752 case ARM::AK_ARMV4:
Logan Chien439e8f92013-12-11 17:16:25 +0000753 setAttributeItem(ARM_ISA_use, Allowed, false);
754 break;
755
Renato Golin35de35d2015-05-12 10:33:58 +0000756 case ARM::AK_ARMV4T:
757 case ARM::AK_ARMV5T:
758 case ARM::AK_ARMV5TE:
759 case ARM::AK_ARMV6:
Logan Chien439e8f92013-12-11 17:16:25 +0000760 setAttributeItem(ARM_ISA_use, Allowed, false);
761 setAttributeItem(THUMB_ISA_use, Allowed, false);
762 break;
763
Renato Golin35de35d2015-05-12 10:33:58 +0000764 case ARM::AK_ARMV6T2:
Logan Chien439e8f92013-12-11 17:16:25 +0000765 setAttributeItem(ARM_ISA_use, Allowed, false);
766 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
767 break;
768
Renato Golin35de35d2015-05-12 10:33:58 +0000769 case ARM::AK_ARMV6K:
Artyom Skrobovf187a652015-11-16 14:05:32 +0000770 case ARM::AK_ARMV6KZ:
Logan Chien439e8f92013-12-11 17:16:25 +0000771 setAttributeItem(ARM_ISA_use, Allowed, false);
772 setAttributeItem(THUMB_ISA_use, Allowed, false);
773 setAttributeItem(Virtualization_use, AllowTZ, false);
774 break;
775
Renato Golin35de35d2015-05-12 10:33:58 +0000776 case ARM::AK_ARMV6M:
Logan Chien439e8f92013-12-11 17:16:25 +0000777 setAttributeItem(THUMB_ISA_use, Allowed, false);
778 break;
779
Renato Golin35de35d2015-05-12 10:33:58 +0000780 case ARM::AK_ARMV7A:
Logan Chien439e8f92013-12-11 17:16:25 +0000781 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
782 setAttributeItem(ARM_ISA_use, Allowed, false);
783 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
784 break;
785
Renato Golin35de35d2015-05-12 10:33:58 +0000786 case ARM::AK_ARMV7R:
Logan Chien439e8f92013-12-11 17:16:25 +0000787 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
788 setAttributeItem(ARM_ISA_use, Allowed, false);
789 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
790 break;
791
Renato Golin35de35d2015-05-12 10:33:58 +0000792 case ARM::AK_ARMV7M:
Logan Chien439e8f92013-12-11 17:16:25 +0000793 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
794 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
795 break;
796
Renato Golin35de35d2015-05-12 10:33:58 +0000797 case ARM::AK_ARMV8A:
798 case ARM::AK_ARMV8_1A:
Oliver Stannard46670712015-12-01 10:33:56 +0000799 case ARM::AK_ARMV8_2A:
Logan Chien439e8f92013-12-11 17:16:25 +0000800 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
801 setAttributeItem(ARM_ISA_use, Allowed, false);
802 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
803 setAttributeItem(MPextension_use, Allowed, false);
804 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
805 break;
806
Bradley Smithe26f7992016-01-15 10:24:39 +0000807 case ARM::AK_ARMV8MBaseline:
808 case ARM::AK_ARMV8MMainline:
809 setAttributeItem(THUMB_ISA_use, AllowThumbDerived, false);
810 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
811 break;
812
Renato Golin35de35d2015-05-12 10:33:58 +0000813 case ARM::AK_IWMMXT:
Logan Chien439e8f92013-12-11 17:16:25 +0000814 setAttributeItem(ARM_ISA_use, Allowed, false);
815 setAttributeItem(THUMB_ISA_use, Allowed, false);
816 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
817 break;
818
Renato Golin35de35d2015-05-12 10:33:58 +0000819 case ARM::AK_IWMMXT2:
Logan Chien439e8f92013-12-11 17:16:25 +0000820 setAttributeItem(ARM_ISA_use, Allowed, false);
821 setAttributeItem(THUMB_ISA_use, Allowed, false);
822 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
823 break;
824
825 default:
826 report_fatal_error("Unknown Arch: " + Twine(Arch));
827 break;
828 }
829}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000830
Logan Chien8cbb80d2013-10-28 17:51:12 +0000831void ARMTargetELFStreamer::emitFPU(unsigned Value) {
832 FPU = Value;
833}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000834
Logan Chien8cbb80d2013-10-28 17:51:12 +0000835void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
836 switch (FPU) {
Renato Golin35de35d2015-05-12 10:33:58 +0000837 case ARM::FK_VFP:
838 case ARM::FK_VFPV2:
Logan Chiena39510a2013-12-18 17:23:15 +0000839 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000840 ARMBuildAttrs::AllowFPv2,
841 /* OverwriteExisting= */ false);
842 break;
843
Renato Golin35de35d2015-05-12 10:33:58 +0000844 case ARM::FK_VFPV3:
Logan Chiena39510a2013-12-18 17:23:15 +0000845 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000846 ARMBuildAttrs::AllowFPv3A,
847 /* OverwriteExisting= */ false);
848 break;
849
Javed Absard5526302015-06-29 09:32:29 +0000850 case ARM::FK_VFPV3_FP16:
851 setAttributeItem(ARMBuildAttrs::FP_arch,
852 ARMBuildAttrs::AllowFPv3A,
853 /* OverwriteExisting= */ false);
854 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
855 ARMBuildAttrs::AllowHPFP,
856 /* OverwriteExisting= */ false);
857 break;
858
Renato Golin35de35d2015-05-12 10:33:58 +0000859 case ARM::FK_VFPV3_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000860 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000861 ARMBuildAttrs::AllowFPv3B,
862 /* OverwriteExisting= */ false);
863 break;
864
Javed Absard5526302015-06-29 09:32:29 +0000865 case ARM::FK_VFPV3_D16_FP16:
866 setAttributeItem(ARMBuildAttrs::FP_arch,
867 ARMBuildAttrs::AllowFPv3B,
868 /* OverwriteExisting= */ false);
869 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
870 ARMBuildAttrs::AllowHPFP,
871 /* OverwriteExisting= */ false);
872 break;
873
874 case ARM::FK_VFPV3XD:
875 setAttributeItem(ARMBuildAttrs::FP_arch,
876 ARMBuildAttrs::AllowFPv3B,
877 /* OverwriteExisting= */ false);
878 break;
879 case ARM::FK_VFPV3XD_FP16:
880 setAttributeItem(ARMBuildAttrs::FP_arch,
881 ARMBuildAttrs::AllowFPv3B,
882 /* OverwriteExisting= */ false);
883 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
884 ARMBuildAttrs::AllowHPFP,
885 /* OverwriteExisting= */ false);
886 break;
887
Renato Golin35de35d2015-05-12 10:33:58 +0000888 case ARM::FK_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000889 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000890 ARMBuildAttrs::AllowFPv4A,
891 /* OverwriteExisting= */ false);
892 break;
893
John Brawn985c04e2015-06-05 13:31:19 +0000894 // ABI_HardFP_use is handled in ARMAsmPrinter, so _SP_D16 is treated the same
895 // as _D16 here.
896 case ARM::FK_FPV4_SP_D16:
Renato Golin35de35d2015-05-12 10:33:58 +0000897 case ARM::FK_VFPV4_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000898 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000899 ARMBuildAttrs::AllowFPv4B,
900 /* OverwriteExisting= */ false);
901 break;
902
Renato Golin35de35d2015-05-12 10:33:58 +0000903 case ARM::FK_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000904 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000905 ARMBuildAttrs::AllowFPARMv8A,
906 /* OverwriteExisting= */ false);
907 break;
908
Oliver Stannard37e4daa2014-10-01 09:02:17 +0000909 // FPV5_D16 is identical to FP_ARMV8 except for the number of D registers, so
910 // uses the FP_ARMV8_D16 build attribute.
John Brawn985c04e2015-06-05 13:31:19 +0000911 case ARM::FK_FPV5_SP_D16:
Renato Golin35de35d2015-05-12 10:33:58 +0000912 case ARM::FK_FPV5_D16:
Oliver Stannard37e4daa2014-10-01 09:02:17 +0000913 setAttributeItem(ARMBuildAttrs::FP_arch,
914 ARMBuildAttrs::AllowFPARMv8B,
915 /* OverwriteExisting= */ false);
916 break;
917
Renato Golin35de35d2015-05-12 10:33:58 +0000918 case ARM::FK_NEON:
Logan Chiena39510a2013-12-18 17:23:15 +0000919 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000920 ARMBuildAttrs::AllowFPv3A,
921 /* OverwriteExisting= */ false);
922 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
923 ARMBuildAttrs::AllowNeon,
924 /* OverwriteExisting= */ false);
925 break;
926
Javed Absard5526302015-06-29 09:32:29 +0000927 case ARM::FK_NEON_FP16:
928 setAttributeItem(ARMBuildAttrs::FP_arch,
929 ARMBuildAttrs::AllowFPv3A,
930 /* OverwriteExisting= */ false);
931 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
932 ARMBuildAttrs::AllowNeon,
933 /* OverwriteExisting= */ false);
934 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
935 ARMBuildAttrs::AllowHPFP,
936 /* OverwriteExisting= */ false);
937 break;
938
Renato Golin35de35d2015-05-12 10:33:58 +0000939 case ARM::FK_NEON_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000940 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000941 ARMBuildAttrs::AllowFPv4A,
942 /* OverwriteExisting= */ false);
943 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
944 ARMBuildAttrs::AllowNeon2,
945 /* OverwriteExisting= */ false);
946 break;
947
Renato Golin35de35d2015-05-12 10:33:58 +0000948 case ARM::FK_NEON_FP_ARMV8:
949 case ARM::FK_CRYPTO_NEON_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000950 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000951 ARMBuildAttrs::AllowFPARMv8A,
952 /* OverwriteExisting= */ false);
Vladimir Sukharevc632cda2015-03-26 17:05:54 +0000953 // 'Advanced_SIMD_arch' must be emitted not here, but within
954 // ARMAsmPrinter::emitAttributes(), depending on hasV8Ops() and hasV8_1a()
Logan Chien8cbb80d2013-10-28 17:51:12 +0000955 break;
956
Renato Golin35de35d2015-05-12 10:33:58 +0000957 case ARM::FK_SOFTVFP:
John Brawn985c04e2015-06-05 13:31:19 +0000958 case ARM::FK_NONE:
Logan Chien05ae7442014-01-02 15:50:02 +0000959 break;
960
Logan Chien8cbb80d2013-10-28 17:51:12 +0000961 default:
962 report_fatal_error("Unknown FPU: " + Twine(FPU));
963 break;
964 }
965}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000966
Logan Chien8cbb80d2013-10-28 17:51:12 +0000967size_t ARMTargetELFStreamer::calculateContentSize() const {
968 size_t Result = 0;
969 for (size_t i = 0; i < Contents.size(); ++i) {
970 AttributeItem item = Contents[i];
971 switch (item.Type) {
972 case AttributeItem::HiddenAttribute:
973 break;
974 case AttributeItem::NumericAttribute:
Logan Chien5b776b72014-02-22 14:00:39 +0000975 Result += getULEB128Size(item.Tag);
976 Result += getULEB128Size(item.IntValue);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000977 break;
978 case AttributeItem::TextAttribute:
Logan Chien5b776b72014-02-22 14:00:39 +0000979 Result += getULEB128Size(item.Tag);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000980 Result += item.StringValue.size() + 1; // string + '\0'
981 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000982 case AttributeItem::NumericAndTextAttributes:
Logan Chien5b776b72014-02-22 14:00:39 +0000983 Result += getULEB128Size(item.Tag);
984 Result += getULEB128Size(item.IntValue);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000985 Result += item.StringValue.size() + 1; // string + '\0';
986 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000987 }
988 }
989 return Result;
990}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000991
Logan Chien8cbb80d2013-10-28 17:51:12 +0000992void ARMTargetELFStreamer::finishAttributeSection() {
993 // <format-version>
994 // [ <section-length> "vendor-name"
995 // [ <file-tag> <size> <attribute>*
996 // | <section-tag> <size> <section-number>* 0 <attribute>*
997 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
998 // ]+
999 // ]*
1000
Renato Golin35de35d2015-05-12 10:33:58 +00001001 if (FPU != ARM::FK_INVALID)
Logan Chien8cbb80d2013-10-28 17:51:12 +00001002 emitFPUDefaultAttributes();
1003
Renato Golin35de35d2015-05-12 10:33:58 +00001004 if (Arch != ARM::AK_INVALID)
Logan Chien439e8f92013-12-11 17:16:25 +00001005 emitArchDefaultAttributes();
1006
Logan Chien8cbb80d2013-10-28 17:51:12 +00001007 if (Contents.empty())
1008 return;
1009
1010 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
1011
1012 ARMELFStreamer &Streamer = getStreamer();
1013
1014 // Switch to .ARM.attributes section
1015 if (AttributeSection) {
1016 Streamer.SwitchSection(AttributeSection);
1017 } else {
Rafael Espindolaba31e272015-01-29 17:33:21 +00001018 AttributeSection = Streamer.getContext().getELFSection(
1019 ".ARM.attributes", ELF::SHT_ARM_ATTRIBUTES, 0);
Logan Chien8cbb80d2013-10-28 17:51:12 +00001020 Streamer.SwitchSection(AttributeSection);
1021
1022 // Format version
1023 Streamer.EmitIntValue(0x41, 1);
1024 }
1025
1026 // Vendor size + Vendor name + '\0'
1027 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
1028
1029 // Tag + Tag Size
1030 const size_t TagHeaderSize = 1 + 4;
1031
1032 const size_t ContentsSize = calculateContentSize();
1033
1034 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
1035 Streamer.EmitBytes(CurrentVendor);
1036 Streamer.EmitIntValue(0, 1); // '\0'
1037
1038 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
1039 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
1040
1041 // Size should have been accounted for already, now
1042 // emit each field as its type (ULEB or String)
1043 for (size_t i = 0; i < Contents.size(); ++i) {
1044 AttributeItem item = Contents[i];
1045 Streamer.EmitULEB128IntValue(item.Tag);
1046 switch (item.Type) {
1047 default: llvm_unreachable("Invalid attribute type");
1048 case AttributeItem::NumericAttribute:
1049 Streamer.EmitULEB128IntValue(item.IntValue);
1050 break;
1051 case AttributeItem::TextAttribute:
Charlie Turner8d433692014-11-27 12:13:56 +00001052 Streamer.EmitBytes(item.StringValue);
Logan Chien8cbb80d2013-10-28 17:51:12 +00001053 Streamer.EmitIntValue(0, 1); // '\0'
1054 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +00001055 case AttributeItem::NumericAndTextAttributes:
1056 Streamer.EmitULEB128IntValue(item.IntValue);
Charlie Turner8d433692014-11-27 12:13:56 +00001057 Streamer.EmitBytes(item.StringValue);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +00001058 Streamer.EmitIntValue(0, 1); // '\0'
1059 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +00001060 }
1061 }
1062
1063 Contents.clear();
Renato Golin35de35d2015-05-12 10:33:58 +00001064 FPU = ARM::FK_INVALID;
Logan Chien8cbb80d2013-10-28 17:51:12 +00001065}
Rafael Espindola4c6f6132014-04-27 17:10:46 +00001066
1067void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
1068 ARMELFStreamer &Streamer = getStreamer();
1069 if (!Streamer.IsThumb)
1070 return;
1071
Rafael Espindolac73aed12015-06-03 19:03:11 +00001072 Streamer.getAssembler().registerSymbol(*Symbol);
Rafael Espindola95fb9b92015-06-02 20:38:46 +00001073 unsigned Type = cast<MCSymbolELF>(Symbol)->getType();
Rafael Espindolaf8794ff2015-06-04 00:47:43 +00001074 if (Type == ELF::STT_FUNC || Type == ELF::STT_GNU_IFUNC)
Rafael Espindola4c6f6132014-04-27 17:10:46 +00001075 Streamer.EmitThumbFunc(Symbol);
1076}
1077
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001078void
1079ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
1080 getStreamer().EmitFixup(S, FK_Data_4);
1081}
Rafael Espindola466d6632014-04-27 20:23:58 +00001082
1083void ARMTargetELFStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
1084 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
Rafael Espindola4a042942014-05-01 12:45:43 +00001085 const MCSymbol &Sym = SRE->getSymbol();
1086 if (!Sym.isDefined()) {
Rafael Espindola466d6632014-04-27 20:23:58 +00001087 getStreamer().EmitAssignment(Symbol, Value);
1088 return;
1089 }
1090 }
1091
1092 getStreamer().EmitThumbFunc(Symbol);
1093 getStreamer().EmitAssignment(Symbol, Value);
1094}
1095
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +00001096void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
1097 getStreamer().emitInst(Inst, Suffix);
1098}
Logan Chien8cbb80d2013-10-28 17:51:12 +00001099
Keno Fischer00021422016-01-12 13:38:15 +00001100void ARMTargetELFStreamer::reset() { AttributeSection = nullptr; }
1101
Logan Chien8cbb80d2013-10-28 17:51:12 +00001102void ARMELFStreamer::FinishImpl() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +00001103 MCTargetStreamer &TS = *getTargetStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +00001104 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1105 ATS.finishAttributeSection();
1106
1107 MCELFStreamer::FinishImpl();
1108}
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001109
Keno Fischer00021422016-01-12 13:38:15 +00001110void ARMELFStreamer::reset() {
1111 MCTargetStreamer &TS = *getTargetStreamer();
1112 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1113 ATS.reset();
1114 MappingSymbolCounter = 0;
1115 MCELFStreamer::reset();
1116 // MCELFStreamer clear's the assembler's e_flags. However, for
1117 // arm we manually set the ABI version on streamer creation, so
1118 // do the same here
1119 getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1120}
1121
Mehdi Amini68c6c8c2016-10-07 08:48:07 +00001122inline void ARMELFStreamer::SwitchToEHSection(StringRef Prefix,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001123 unsigned Type,
1124 unsigned Flags,
1125 SectionKind Kind,
1126 const MCSymbol &Fn) {
1127 const MCSectionELF &FnSection =
1128 static_cast<const MCSectionELF &>(Fn.getSection());
1129
1130 // Create the name for new section
1131 StringRef FnSecName(FnSection.getSectionName());
1132 SmallString<128> EHSecName(Prefix);
1133 if (FnSecName != ".text") {
1134 EHSecName += FnSecName;
1135 }
1136
1137 // Get .ARM.extab or .ARM.exidx section
Rafael Espindola0ccf9b72015-06-02 21:30:13 +00001138 const MCSymbolELF *Group = FnSection.getGroup();
Rafael Espindola61e8ce32015-04-06 04:25:18 +00001139 if (Group)
1140 Flags |= ELF::SHF_GROUP;
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001141 MCSectionELF *EHSection =
Rafael Espindola61e8ce32015-04-06 04:25:18 +00001142 getContext().getELFSection(EHSecName, Type, Flags, 0, Group,
1143 FnSection.getUniqueID(), nullptr, &FnSection);
1144
Logan Chiend8bb4b72013-04-16 12:02:21 +00001145 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001146
1147 // Switch to .ARM.extab or .ARM.exidx section
1148 SwitchSection(EHSection);
Rafael Espindola7b514962014-02-04 18:34:04 +00001149 EmitCodeAlignment(4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001150}
1151
1152inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
Rafael Espindola449711c2015-11-18 06:02:15 +00001153 SwitchToEHSection(".ARM.extab", ELF::SHT_PROGBITS, ELF::SHF_ALLOC,
1154 SectionKind::getData(), FnStart);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001155}
1156
1157inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
Rafael Espindola449711c2015-11-18 06:02:15 +00001158 SwitchToEHSection(".ARM.exidx", ELF::SHT_ARM_EXIDX,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001159 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
Rafael Espindola449711c2015-11-18 06:02:15 +00001160 SectionKind::getData(), FnStart);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001161}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +00001162
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001163void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1164 MCDataFragment *Frag = getOrCreateDataFragment();
Jim Grosbach63661f82015-05-15 19:13:05 +00001165 Frag->getFixups().push_back(MCFixup::create(Frag->getContents().size(), Expr,
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001166 Kind));
1167}
Logan Chien2bcc42c2013-01-30 15:39:04 +00001168
Keno Fischer00021422016-01-12 13:38:15 +00001169void ARMELFStreamer::EHReset() {
Craig Topper062a2ba2014-04-25 05:30:21 +00001170 ExTab = nullptr;
1171 FnStart = nullptr;
1172 Personality = nullptr;
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001173 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
Logan Chien325823a2013-06-09 12:22:30 +00001174 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001175 FPOffset = 0;
1176 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +00001177 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001178 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001179 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001180
Logan Chien325823a2013-06-09 12:22:30 +00001181 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001182 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001183}
1184
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001185void ARMELFStreamer::emitFnStart() {
Craig Toppere73658d2014-04-28 04:05:08 +00001186 assert(FnStart == nullptr);
Jim Grosbach6f482002015-05-18 18:43:14 +00001187 FnStart = getContext().createTempSymbol();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001188 EmitLabel(FnStart);
1189}
1190
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001191void ARMELFStreamer::emitFnEnd() {
Alp Tokercb402912014-01-24 17:20:08 +00001192 assert(FnStart && ".fnstart must precedes .fnend");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001193
1194 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +00001195 if (!ExTab && !CantUnwind)
1196 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001197
1198 // Emit the exception index table entry
1199 SwitchToExIdxSection(*FnStart);
1200
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001201 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
Logan Chiend8bb4b72013-04-16 12:02:21 +00001202 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001203
1204 const MCSymbolRefExpr *FnStartRef =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001205 MCSymbolRefExpr::create(FnStart,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001206 MCSymbolRefExpr::VK_ARM_PREL31,
1207 getContext());
1208
Rafael Espindola64e1af82013-07-02 15:49:13 +00001209 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001210
1211 if (CantUnwind) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001212 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001213 } else if (ExTab) {
1214 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001215 const MCSymbolRefExpr *ExTabEntryRef =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001216 MCSymbolRefExpr::create(ExTab,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001217 MCSymbolRefExpr::VK_ARM_PREL31,
1218 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +00001219 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001220 } else {
1221 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1222 // the second word of exception index table entry. The size of the unwind
1223 // opcodes should always be 4 bytes.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001224 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
Jonathan Roelofs4971b402014-05-15 02:24:50 +00001225 "Compact model must use __aeabi_unwind_cpp_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +00001226 assert(Opcodes.size() == 4u &&
Jonathan Roelofs4971b402014-05-15 02:24:50 +00001227 "Unwind opcode size for __aeabi_unwind_cpp_pr0 must be equal to 4");
Christian Pirker39db7ec2014-05-13 16:44:30 +00001228 uint64_t Intval = Opcodes[0] |
1229 Opcodes[1] << 8 |
1230 Opcodes[2] << 16 |
1231 Opcodes[3] << 24;
1232 EmitIntValue(Intval, Opcodes.size());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001233 }
1234
Logan Chien4ea23b52013-05-10 16:17:24 +00001235 // Switch to the section containing FnStart
1236 SwitchSection(&FnStart->getSection());
1237
Logan Chien2bcc42c2013-01-30 15:39:04 +00001238 // Clean exception handling frame information
Keno Fischer00021422016-01-12 13:38:15 +00001239 EHReset();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001240}
1241
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001242void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1243
1244// Add the R_ARM_NONE fixup at the same position
1245void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
Jim Grosbach6f482002015-05-18 18:43:14 +00001246 const MCSymbol *PersonalitySym = getContext().getOrCreateSymbol(Name);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001247
Jim Grosbach13760bd2015-05-30 01:25:56 +00001248 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::create(
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001249 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1250
Rafael Espindola2be12812014-06-25 15:29:54 +00001251 visitUsedExpr(*PersonalityRef);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001252 MCDataFragment *DF = getOrCreateDataFragment();
Jim Grosbach63661f82015-05-15 19:13:05 +00001253 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001254 PersonalityRef,
1255 MCFixup::getKindForSize(4, false)));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001256}
1257
Logan Chien325823a2013-06-09 12:22:30 +00001258void ARMELFStreamer::FlushPendingOffset() {
1259 if (PendingOffset != 0) {
1260 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1261 PendingOffset = 0;
1262 }
1263}
1264
Logan Chienc931fce2013-07-02 12:43:27 +00001265void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +00001266 // Emit the unwind opcode to restore $sp.
1267 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001268 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +00001269 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1270 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001271 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +00001272 } else {
1273 FlushPendingOffset();
1274 }
1275
1276 // Finalize the unwind opcode sequence
1277 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +00001278
1279 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1280 // section. Thus, we don't have to create an entry in the .ARM.extab
1281 // section.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001282 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +00001283 return;
1284
1285 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001286 SwitchToExTabSection(*FnStart);
1287
1288 // Create .ARM.extab label for offset in .ARM.exidx
1289 assert(!ExTab);
Jim Grosbach6f482002015-05-18 18:43:14 +00001290 ExTab = getContext().createTempSymbol();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001291 EmitLabel(ExTab);
1292
Logan Chien4ea23b52013-05-10 16:17:24 +00001293 // Emit personality
1294 if (Personality) {
1295 const MCSymbolRefExpr *PersonalityRef =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001296 MCSymbolRefExpr::create(Personality,
Logan Chien4ea23b52013-05-10 16:17:24 +00001297 MCSymbolRefExpr::VK_ARM_PREL31,
1298 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001299
Rafael Espindola64e1af82013-07-02 15:49:13 +00001300 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +00001301 }
Logan Chien2bcc42c2013-01-30 15:39:04 +00001302
1303 // Emit unwind opcodes
Christian Pirker39db7ec2014-05-13 16:44:30 +00001304 assert((Opcodes.size() % 4) == 0 &&
1305 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be multiple of 4");
1306 for (unsigned I = 0; I != Opcodes.size(); I += 4) {
1307 uint64_t Intval = Opcodes[I] |
1308 Opcodes[I + 1] << 8 |
1309 Opcodes[I + 2] << 16 |
1310 Opcodes[I + 3] << 24;
1311 EmitIntValue(Intval, 4);
1312 }
Logan Chienc931fce2013-07-02 12:43:27 +00001313
1314 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1315 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1316 // after the unwind opcodes. The handler data consists of several 32-bit
1317 // words, and should be terminated by zero.
1318 //
1319 // In case that the .handlerdata directive is not specified by the
1320 // programmer, we should emit zero to terminate the handler data.
1321 if (NoHandlerData && !Personality)
1322 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001323}
1324
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001325void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
Logan Chien4ea23b52013-05-10 16:17:24 +00001326
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001327void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001328 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001329 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001330}
1331
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +00001332void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1333 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1334 PersonalityIndex = Index;
1335}
1336
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001337void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001338 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001339 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001340 "the operand of .setfp directive should be either $sp or $fp");
1341
1342 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +00001343 FPReg = NewFPReg;
1344
1345 if (NewSPReg == ARM::SP)
1346 FPOffset = SPOffset + Offset;
1347 else
1348 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001349}
1350
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +00001351void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
1352 assert((Reg != ARM::SP && Reg != ARM::PC) &&
1353 "the operand of .movsp cannot be either sp or pc");
1354 assert(FPReg == ARM::SP && "current FP must be SP");
1355
1356 FlushPendingOffset();
1357
1358 FPReg = Reg;
1359 FPOffset = SPOffset + Offset;
1360
1361 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1362 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1363}
1364
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001365void ARMELFStreamer::emitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001366 // Track the change of the $sp offset
1367 SPOffset -= Offset;
1368
1369 // To squash multiple .pad directives, we should delay the unwind opcode
1370 // until the .save, .vsave, .handlerdata, or .fnend directives.
1371 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001372}
1373
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001374void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001375 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +00001376 // Collect the registers in the register list
1377 unsigned Count = 0;
1378 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +00001379 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001380 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001381 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +00001382 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +00001383 unsigned Bit = (1u << Reg);
1384 if ((Mask & Bit) == 0) {
1385 Mask |= Bit;
1386 ++Count;
1387 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001388 }
Logan Chien325823a2013-06-09 12:22:30 +00001389
1390 // Track the change the $sp offset: For the .save directive, the
1391 // corresponding push instruction will decrease the $sp by (4 * Count).
1392 // For the .vsave directive, the corresponding vpush instruction will
1393 // decrease $sp by (8 * Count).
1394 SPOffset -= Count * (IsVector ? 8 : 4);
1395
1396 // Emit the opcode
1397 FlushPendingOffset();
1398 if (IsVector)
1399 UnwindOpAsm.EmitVFPRegSave(Mask);
1400 else
1401 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001402}
1403
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +00001404void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1405 const SmallVectorImpl<uint8_t> &Opcodes) {
1406 FlushPendingOffset();
1407 SPOffset = SPOffset - Offset;
1408 UnwindOpAsm.EmitRaw(Opcodes);
1409}
1410
Tim Northover5cc3dc82012-12-07 16:50:23 +00001411namespace llvm {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001412
Rafael Espindola73870dd2015-03-16 21:43:42 +00001413MCTargetStreamer *createARMTargetAsmStreamer(MCStreamer &S,
1414 formatted_raw_ostream &OS,
1415 MCInstPrinter *InstPrint,
1416 bool isVerboseAsm) {
1417 return new ARMTargetAsmStreamer(S, OS, *InstPrint, isVerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001418}
1419
Peter Collingbourne20c72592015-02-19 00:45:02 +00001420MCTargetStreamer *createARMNullTargetStreamer(MCStreamer &S) {
1421 return new ARMTargetStreamer(S);
1422}
1423
Rafael Espindolacd584a82015-03-19 01:50:16 +00001424MCTargetStreamer *createARMObjectTargetStreamer(MCStreamer &S,
1425 const MCSubtargetInfo &STI) {
Daniel Sanders50f17232015-09-15 16:17:27 +00001426 const Triple &TT = STI.getTargetTriple();
Daniel Sandersfbdab432015-07-06 16:33:18 +00001427 if (TT.isOSBinFormatELF())
Rafael Espindolacd584a82015-03-19 01:50:16 +00001428 return new ARMTargetELFStreamer(S);
1429 return new ARMTargetStreamer(S);
1430}
1431
Rafael Espindola7b61ddf2014-10-15 16:12:52 +00001432MCELFStreamer *createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001433 raw_pwrite_stream &OS,
1434 MCCodeEmitter *Emitter, bool RelaxAll,
1435 bool IsThumb) {
Rafael Espindola24ea09e2014-01-26 06:06:37 +00001436 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001437 // FIXME: This should eventually end up somewhere else where more
1438 // intelligent flag decisions can be made. For now we are just maintaining
1439 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1440 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1441
Tim Northover5cc3dc82012-12-07 16:50:23 +00001442 if (RelaxAll)
1443 S->getAssembler().setRelaxAll(true);
Tim Northover5cc3dc82012-12-07 16:50:23 +00001444 return S;
Alexander Kornienkof00654e2015-06-23 09:49:53 +00001445}
Tim Northover5cc3dc82012-12-07 16:50:23 +00001446
Eugene Zelenkoe79c0772017-01-27 23:58:02 +00001447} // end namespace llvm