blob: cff772ae584ae093e77f169c5932557c86daa81a [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"
Zachary Turner264b5d92017-06-07 03:48:56 +000024#include "llvm/BinaryFormat/ELF.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000025#include "llvm/MC/MCAsmBackend.h"
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +000026#include "llvm/MC/MCAsmInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000027#include "llvm/MC/MCAssembler.h"
28#include "llvm/MC/MCCodeEmitter.h"
29#include "llvm/MC/MCContext.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000030#include "llvm/MC/MCELFStreamer.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000031#include "llvm/MC/MCExpr.h"
Eugene Zelenkoe79c0772017-01-27 23:58:02 +000032#include "llvm/MC/MCFixup.h"
33#include "llvm/MC/MCFragment.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000034#include "llvm/MC/MCInst.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000035#include "llvm/MC/MCInstPrinter.h"
Peter Collingbournef7b81db2018-05-18 18:26:45 +000036#include "llvm/MC/MCObjectWriter.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000037#include "llvm/MC/MCRegisterInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000038#include "llvm/MC/MCSection.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000039#include "llvm/MC/MCSectionELF.h"
40#include "llvm/MC/MCStreamer.h"
Eugene Zelenkoe79c0772017-01-27 23:58:02 +000041#include "llvm/MC/MCSubtargetInfo.h"
42#include "llvm/MC/MCSymbol.h"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000043#include "llvm/MC/MCSymbolELF.h"
Eugene Zelenkoe79c0772017-01-27 23:58:02 +000044#include "llvm/MC/SectionKind.h"
Saleem Abdulrasool278a9f42014-01-19 08:25:27 +000045#include "llvm/Support/ARMBuildAttributes.h"
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +000046#include "llvm/Support/ARMEHABI.h"
Eugene Zelenkoe79c0772017-01-27 23:58:02 +000047#include "llvm/Support/Casting.h"
Eugene Zelenkoe79c0772017-01-27 23:58:02 +000048#include "llvm/Support/ErrorHandling.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000049#include "llvm/Support/FormattedStream.h"
Logan Chien5b776b72014-02-22 14:00:39 +000050#include "llvm/Support/LEB128.h"
Eugene Zelenkoe79c0772017-01-27 23:58:02 +000051#include "llvm/Support/TargetParser.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000052#include "llvm/Support/raw_ostream.h"
Logan Chien8cbb80d2013-10-28 17:51:12 +000053#include <algorithm>
Eugene Zelenkoe79c0772017-01-27 23:58:02 +000054#include <cassert>
55#include <climits>
56#include <cstddef>
57#include <cstdint>
58#include <string>
Tim Northover5cc3dc82012-12-07 16:50:23 +000059
60using namespace llvm;
61
Logan Chiend8bb4b72013-04-16 12:02:21 +000062static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +000063 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX &&
64 "Invalid personality index");
Logan Chiend8bb4b72013-04-16 12:02:21 +000065 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
66}
67
Tim Northover5cc3dc82012-12-07 16:50:23 +000068namespace {
69
Rafael Espindolaa17151a2013-10-08 13:08:17 +000070class ARMELFStreamer;
71
72class ARMTargetAsmStreamer : public ARMTargetStreamer {
73 formatted_raw_ostream &OS;
74 MCInstPrinter &InstPrinter;
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +000075 bool IsVerboseAsm;
Rafael Espindolaa17151a2013-10-08 13:08:17 +000076
Craig Topperca7e3e52014-03-10 03:19:03 +000077 void emitFnStart() override;
78 void emitFnEnd() override;
79 void emitCantUnwind() override;
80 void emitPersonality(const MCSymbol *Personality) override;
81 void emitPersonalityIndex(unsigned Index) override;
82 void emitHandlerData() override;
83 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
84 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
85 void emitPad(int64_t Offset) override;
86 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
87 bool isVector) override;
88 void emitUnwindRaw(int64_t Offset,
89 const SmallVectorImpl<uint8_t> &Opcodes) override;
Rafael Espindolaa17151a2013-10-08 13:08:17 +000090
Craig Topperca7e3e52014-03-10 03:19:03 +000091 void switchVendor(StringRef Vendor) override;
92 void emitAttribute(unsigned Attribute, unsigned Value) override;
93 void emitTextAttribute(unsigned Attribute, StringRef String) override;
94 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
Artyom Skrobove9b3fb82015-12-07 14:22:39 +000095 StringRef StringValue) override;
Florian Hahn67ddd1d2017-07-27 16:27:56 +000096 void emitArch(ARM::ArchKind Arch) override;
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +000097 void emitArchExtension(unsigned ArchExt) override;
Florian Hahn67ddd1d2017-07-27 16:27:56 +000098 void emitObjectArch(ARM::ArchKind Arch) override;
Craig Topperca7e3e52014-03-10 03:19:03 +000099 void emitFPU(unsigned FPU) override;
100 void emitInst(uint32_t Inst, char Suffix = '\0') override;
101 void finishAttributeSection() override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000102
Craig Topperca7e3e52014-03-10 03:19:03 +0000103 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
Rafael Espindola466d6632014-04-27 20:23:58 +0000104 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000105
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000106public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000107 ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
108 MCInstPrinter &InstPrinter, bool VerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000109};
110
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000111ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
112 formatted_raw_ostream &OS,
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000113 MCInstPrinter &InstPrinter,
114 bool VerboseAsm)
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000115 : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
116 IsVerboseAsm(VerboseAsm) {}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000117
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000118void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
119void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
120void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000121
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000122void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
123 OS << "\t.personality " << Personality->getName() << '\n';
124}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000125
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000126void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
127 OS << "\t.personalityindex " << Index << '\n';
128}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000129
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000130void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000131
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000132void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
133 int64_t Offset) {
134 OS << "\t.setfp\t";
135 InstPrinter.printRegName(OS, FpReg);
136 OS << ", ";
137 InstPrinter.printRegName(OS, SpReg);
138 if (Offset)
139 OS << ", #" << Offset;
140 OS << '\n';
141}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000142
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000143void ARMTargetAsmStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
144 assert((Reg != ARM::SP && Reg != ARM::PC) &&
145 "the operand of .movsp cannot be either sp or pc");
146
147 OS << "\t.movsp\t";
148 InstPrinter.printRegName(OS, Reg);
149 if (Offset)
150 OS << ", #" << Offset;
151 OS << '\n';
152}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000153
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000154void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
155 OS << "\t.pad\t#" << Offset << '\n';
156}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000157
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000158void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
159 bool isVector) {
160 assert(RegList.size() && "RegList should not be empty");
161 if (isVector)
162 OS << "\t.vsave\t{";
163 else
164 OS << "\t.save\t{";
165
166 InstPrinter.printRegName(OS, RegList[0]);
167
168 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
169 OS << ", ";
170 InstPrinter.printRegName(OS, RegList[i]);
171 }
172
173 OS << "}\n";
174}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000175
176void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {}
177
Logan Chien8cbb80d2013-10-28 17:51:12 +0000178void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000179 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
180 if (IsVerboseAsm) {
181 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
182 if (!Name.empty())
183 OS << "\t@ " << Name;
184 }
185 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000186}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000187
Logan Chien8cbb80d2013-10-28 17:51:12 +0000188void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
189 StringRef String) {
190 switch (Attribute) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000191 case ARMBuildAttrs::CPU_name:
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000192 OS << "\t.cpu\t" << String.lower();
193 break;
194 default:
195 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000196 if (IsVerboseAsm) {
197 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
198 if (!Name.empty())
199 OS << "\t@ " << Name;
200 }
Logan Chien8cbb80d2013-10-28 17:51:12 +0000201 break;
202 }
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000203 OS << "\n";
204}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000205
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000206void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
207 unsigned IntValue,
208 StringRef StringValue) {
209 switch (Attribute) {
210 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
211 case ARMBuildAttrs::compatibility:
212 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
213 if (!StringValue.empty())
214 OS << ", \"" << StringValue << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000215 if (IsVerboseAsm)
216 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000217 break;
218 }
219 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000220}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000221
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000222void ARMTargetAsmStreamer::emitArch(ARM::ArchKind Arch) {
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000223 OS << "\t.arch\t" << ARM::getArchName(Arch) << "\n";
Logan Chien439e8f92013-12-11 17:16:25 +0000224}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000225
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +0000226void ARMTargetAsmStreamer::emitArchExtension(unsigned ArchExt) {
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000227 OS << "\t.arch_extension\t" << ARM::getArchExtName(ArchExt) << "\n";
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +0000228}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000229
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000230void ARMTargetAsmStreamer::emitObjectArch(ARM::ArchKind Arch) {
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000231 OS << "\t.object_arch\t" << ARM::getArchName(Arch) << '\n';
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000232}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000233
Logan Chien8cbb80d2013-10-28 17:51:12 +0000234void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000235 OS << "\t.fpu\t" << ARM::getFPUName(FPU) << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000236}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000237
238void ARMTargetAsmStreamer::finishAttributeSection() {}
239
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000240void
241ARMTargetAsmStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
242 OS << "\t.tlsdescseq\t" << S->getSymbol().getName();
243}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000244
Rafael Espindola466d6632014-04-27 20:23:58 +0000245void ARMTargetAsmStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
Matt Arsenault8b643552015-06-09 00:31:39 +0000246 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
247
248 OS << "\t.thumb_set\t";
249 Symbol->print(OS, MAI);
250 OS << ", ";
251 Value->print(OS, MAI);
252 OS << '\n';
Rafael Espindola466d6632014-04-27 20:23:58 +0000253}
254
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000255void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
256 OS << "\t.inst";
257 if (Suffix)
258 OS << "." << Suffix;
Benjamin Kramerbe48c402015-05-23 16:39:10 +0000259 OS << "\t0x" << Twine::utohexstr(Inst) << "\n";
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000260}
261
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000262void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
263 const SmallVectorImpl<uint8_t> &Opcodes) {
264 OS << "\t.unwind_raw " << Offset;
265 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
266 OCE = Opcodes.end();
267 OCI != OCE; ++OCI)
Benjamin Kramerbe48c402015-05-23 16:39:10 +0000268 OS << ", 0x" << Twine::utohexstr(*OCI);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000269 OS << '\n';
270}
271
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000272class ARMTargetELFStreamer : public ARMTargetStreamer {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000273private:
274 // This structure holds all attributes, accounting for
Artyom Skrobove9b3fb82015-12-07 14:22:39 +0000275 // their string/numeric value, so we can later emit them
Logan Chien8cbb80d2013-10-28 17:51:12 +0000276 // in declaration order, keeping all in the same vector
277 struct AttributeItem {
278 enum {
279 HiddenAttribute = 0,
280 NumericAttribute,
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000281 TextAttribute,
282 NumericAndTextAttributes
Logan Chien8cbb80d2013-10-28 17:51:12 +0000283 } Type;
284 unsigned Tag;
285 unsigned IntValue;
Artyom Skrobove9b3fb82015-12-07 14:22:39 +0000286 std::string StringValue;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000287
288 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
Charlie Turner8b2caa42015-01-05 13:12:17 +0000289 // The conformance tag must be emitted first when serialised
290 // into an object file. Specifically, the addenda to the ARM ABI
291 // states that (2.3.7.4):
292 //
293 // "To simplify recognition by consumers in the common case of
294 // claiming conformity for the whole file, this tag should be
295 // emitted first in a file-scope sub-subsection of the first
296 // public subsection of the attributes section."
297 //
298 // So it is special-cased in this comparison predicate when the
299 // attributes are sorted in finishAttributeSection().
300 return (RHS.Tag != ARMBuildAttrs::conformance) &&
301 ((LHS.Tag == ARMBuildAttrs::conformance) || (LHS.Tag < RHS.Tag));
Logan Chien8cbb80d2013-10-28 17:51:12 +0000302 }
303 };
304
305 StringRef CurrentVendor;
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000306 unsigned FPU = ARM::FK_INVALID;
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000307 ARM::ArchKind Arch = ARM::ArchKind::INVALID;
308 ARM::ArchKind EmittedArch = ARM::ArchKind::INVALID;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000309 SmallVector<AttributeItem, 64> Contents;
310
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000311 MCSection *AttributeSection = nullptr;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000312
Logan Chien8cbb80d2013-10-28 17:51:12 +0000313 AttributeItem *getAttributeItem(unsigned Attribute) {
314 for (size_t i = 0; i < Contents.size(); ++i)
315 if (Contents[i].Tag == Attribute)
316 return &Contents[i];
Craig Topper062a2ba2014-04-25 05:30:21 +0000317 return nullptr;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000318 }
319
320 void setAttributeItem(unsigned Attribute, unsigned Value,
321 bool OverwriteExisting) {
322 // Look for existing attribute item
323 if (AttributeItem *Item = getAttributeItem(Attribute)) {
324 if (!OverwriteExisting)
325 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000326 Item->Type = AttributeItem::NumericAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000327 Item->IntValue = Value;
328 return;
329 }
330
331 // Create new attribute item
332 AttributeItem Item = {
333 AttributeItem::NumericAttribute,
334 Attribute,
335 Value,
336 StringRef("")
337 };
338 Contents.push_back(Item);
339 }
340
341 void setAttributeItem(unsigned Attribute, StringRef Value,
342 bool OverwriteExisting) {
343 // Look for existing attribute item
344 if (AttributeItem *Item = getAttributeItem(Attribute)) {
345 if (!OverwriteExisting)
346 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000347 Item->Type = AttributeItem::TextAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000348 Item->StringValue = Value;
349 return;
350 }
351
352 // Create new attribute item
353 AttributeItem Item = {
354 AttributeItem::TextAttribute,
355 Attribute,
356 0,
357 Value
358 };
359 Contents.push_back(Item);
360 }
361
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000362 void setAttributeItems(unsigned Attribute, unsigned IntValue,
363 StringRef StringValue, bool OverwriteExisting) {
364 // Look for existing attribute item
365 if (AttributeItem *Item = getAttributeItem(Attribute)) {
366 if (!OverwriteExisting)
367 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000368 Item->Type = AttributeItem::NumericAndTextAttributes;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000369 Item->IntValue = IntValue;
370 Item->StringValue = StringValue;
371 return;
372 }
373
374 // Create new attribute item
375 AttributeItem Item = {
376 AttributeItem::NumericAndTextAttributes,
377 Attribute,
378 IntValue,
379 StringValue
380 };
381 Contents.push_back(Item);
382 }
383
Logan Chien439e8f92013-12-11 17:16:25 +0000384 void emitArchDefaultAttributes();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000385 void emitFPUDefaultAttributes();
386
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000387 ARMELFStreamer &getStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000388
Craig Topperca7e3e52014-03-10 03:19:03 +0000389 void emitFnStart() override;
390 void emitFnEnd() override;
391 void emitCantUnwind() override;
392 void emitPersonality(const MCSymbol *Personality) override;
393 void emitPersonalityIndex(unsigned Index) override;
394 void emitHandlerData() override;
395 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
396 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
397 void emitPad(int64_t Offset) override;
398 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
399 bool isVector) override;
400 void emitUnwindRaw(int64_t Offset,
401 const SmallVectorImpl<uint8_t> &Opcodes) override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000402
Craig Topperca7e3e52014-03-10 03:19:03 +0000403 void switchVendor(StringRef Vendor) override;
404 void emitAttribute(unsigned Attribute, unsigned Value) override;
405 void emitTextAttribute(unsigned Attribute, StringRef String) override;
406 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
407 StringRef StringValue) override;
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000408 void emitArch(ARM::ArchKind Arch) override;
409 void emitObjectArch(ARM::ArchKind Arch) override;
Craig Topperca7e3e52014-03-10 03:19:03 +0000410 void emitFPU(unsigned FPU) override;
411 void emitInst(uint32_t Inst, char Suffix = '\0') override;
412 void finishAttributeSection() override;
Rafael Espindola4c6f6132014-04-27 17:10:46 +0000413 void emitLabel(MCSymbol *Symbol) override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000414
Craig Topperca7e3e52014-03-10 03:19:03 +0000415 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
Rafael Espindola466d6632014-04-27 20:23:58 +0000416 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000417
Logan Chien8cbb80d2013-10-28 17:51:12 +0000418 size_t calculateContentSize() const;
419
Keno Fischer00021422016-01-12 13:38:15 +0000420 // Reset state between object emissions
421 void reset() override;
422
Logan Chien8cbb80d2013-10-28 17:51:12 +0000423public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000424 ARMTargetELFStreamer(MCStreamer &S)
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000425 : ARMTargetStreamer(S), CurrentVendor("aeabi") {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000426};
427
Tim Northover5cc3dc82012-12-07 16:50:23 +0000428/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
429/// the appropriate points in the object files. These symbols are defined in the
430/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
431///
432/// In brief: $a, $t or $d should be emitted at the start of each contiguous
433/// region of ARM code, Thumb code or data in a section. In practice, this
434/// emission does not rely on explicit assembler directives but on inherent
435/// properties of the directives doing the emission (e.g. ".byte" is data, "add
436/// r0, r0, r0" an instruction).
437///
438/// As a result this system is orthogonal to the DataRegion infrastructure used
439/// by MachO. Beware!
440class ARMELFStreamer : public MCELFStreamer {
441public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000442 friend class ARMTargetELFStreamer;
443
Lang Hames02d33052017-10-11 01:57:21 +0000444 ARMELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
Peter Collingbournef7b81db2018-05-18 18:26:45 +0000445 std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter,
Lang Hames2241ffa2017-10-11 23:34:47 +0000446 bool IsThumb)
Peter Collingbournef7b81db2018-05-18 18:26:45 +0000447 : MCELFStreamer(Context, std::move(TAB), std::move(OW), std::move(Emitter)),
Lang Hames2241ffa2017-10-11 23:34:47 +0000448 IsThumb(IsThumb) {
Keno Fischer00021422016-01-12 13:38:15 +0000449 EHReset();
Logan Chiend8bb4b72013-04-16 12:02:21 +0000450 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000451
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000452 ~ARMELFStreamer() override = default;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000453
Craig Topperca7e3e52014-03-10 03:19:03 +0000454 void FinishImpl() override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000455
Logan Chien2bcc42c2013-01-30 15:39:04 +0000456 // ARM exception handling directives
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000457 void emitFnStart();
458 void emitFnEnd();
459 void emitCantUnwind();
460 void emitPersonality(const MCSymbol *Per);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000461 void emitPersonalityIndex(unsigned index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000462 void emitHandlerData();
463 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000464 void emitMovSP(unsigned Reg, int64_t Offset = 0);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000465 void emitPad(int64_t Offset);
466 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000467 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000468
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000469 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
John Brawn66719f62017-04-20 10:18:13 +0000470 LastMappingSymbols[getCurrentSection().first] = std::move(LastEMSInfo);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000471 MCELFStreamer::ChangeSection(Section, Subsection);
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000472 auto LastMappingSymbol = LastMappingSymbols.find(Section);
473 if (LastMappingSymbol != LastMappingSymbols.end()) {
474 LastEMSInfo = std::move(LastMappingSymbol->second);
475 return;
476 }
477 LastEMSInfo.reset(new ElfMappingSymbolInfo(SMLoc(), nullptr, 0));
Tim Northover5cc3dc82012-12-07 16:50:23 +0000478 }
479
480 /// This function is the one used to emit instruction data into the ELF
481 /// streamer. We override it to add the appropriate mapping symbol if
482 /// necessary.
Andrew V. Tischenko75745d02017-04-14 07:44:23 +0000483 void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
484 bool) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000485 if (IsThumb)
486 EmitThumbMappingSymbol();
487 else
488 EmitARMMappingSymbol();
489
David Woodhousee6c13e42014-01-28 23:12:42 +0000490 MCELFStreamer::EmitInstruction(Inst, STI);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000491 }
492
Craig Topperd25ff6f2014-03-10 03:22:59 +0000493 void emitInst(uint32_t Inst, char Suffix) {
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000494 unsigned Size;
495 char Buffer[4];
496 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
497
498 switch (Suffix) {
499 case '\0':
500 Size = 4;
501
502 assert(!IsThumb);
503 EmitARMMappingSymbol();
504 for (unsigned II = 0, IE = Size; II != IE; II++) {
505 const unsigned I = LittleEndian ? (Size - II - 1) : II;
506 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
507 }
508
509 break;
510 case 'n':
511 case 'w':
512 Size = (Suffix == 'n' ? 2 : 4);
513
514 assert(IsThumb);
515 EmitThumbMappingSymbol();
Oliver Stannard7d9198b2018-01-04 13:56:40 +0000516 // Thumb wide instructions are emitted as a pair of 16-bit words of the
517 // appropriate endianness.
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000518 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
Oliver Stannard7d9198b2018-01-04 13:56:40 +0000519 const unsigned I0 = LittleEndian ? II + 0 : II + 1;
520 const unsigned I1 = LittleEndian ? II + 1 : II + 0;
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000521 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
522 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
523 }
524
525 break;
526 default:
527 llvm_unreachable("Invalid Suffix");
528 }
529
530 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
531 }
532
Tim Northover5cc3dc82012-12-07 16:50:23 +0000533 /// This is one of the functions used to emit data into an ELF section, so the
534 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
535 /// necessary.
Craig Topperca7e3e52014-03-10 03:19:03 +0000536 void EmitBytes(StringRef Data) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000537 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000538 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000539 }
540
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000541 void FlushPendingMappingSymbol() {
542 if (!LastEMSInfo->hasInfo())
543 return;
544 ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
545 EmitMappingSymbol("$d", EMS->Loc, EMS->F, EMS->Offset);
546 EMS->resetInfo();
547 }
548
Tim Northover5cc3dc82012-12-07 16:50:23 +0000549 /// This is one of the functions used to emit data into an ELF section, so the
550 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
551 /// necessary.
Craig Topper3c76c522015-09-20 23:35:59 +0000552 void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000553 if (const MCSymbolRefExpr *SRE = dyn_cast_or_null<MCSymbolRefExpr>(Value)) {
Oliver Stannard9be59af2015-11-17 10:00:43 +0000554 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_SBREL && !(Size == 4)) {
555 getContext().reportError(Loc, "relocated expression must be 32-bit");
556 return;
557 }
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000558 getOrCreateDataFragment();
559 }
Saleem Abdulrasoolfe781972015-01-11 04:39:18 +0000560
Tim Northover5cc3dc82012-12-07 16:50:23 +0000561 EmitDataMappingSymbol();
Oliver Stannard09be0602015-11-16 16:22:47 +0000562 MCELFStreamer::EmitValueImpl(Value, Size, Loc);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000563 }
564
Craig Topperca7e3e52014-03-10 03:19:03 +0000565 void EmitAssemblerFlag(MCAssemblerFlag Flag) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000566 MCELFStreamer::EmitAssemblerFlag(Flag);
567
568 switch (Flag) {
569 case MCAF_SyntaxUnified:
570 return; // no-op here.
571 case MCAF_Code16:
572 IsThumb = true;
573 return; // Change to Thumb mode
574 case MCAF_Code32:
575 IsThumb = false;
576 return; // Change to ARM mode
577 case MCAF_Code64:
578 return;
579 case MCAF_SubsectionsViaSymbols:
580 return;
581 }
582 }
583
584private:
585 enum ElfMappingSymbol {
586 EMS_None,
587 EMS_ARM,
588 EMS_Thumb,
589 EMS_Data
590 };
591
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000592 struct ElfMappingSymbolInfo {
593 explicit ElfMappingSymbolInfo(SMLoc Loc, MCFragment *F, uint64_t O)
594 : Loc(Loc), F(F), Offset(O), State(EMS_None) {}
595 void resetInfo() {
596 F = nullptr;
597 Offset = 0;
598 }
599 bool hasInfo() { return F != nullptr; }
600 SMLoc Loc;
601 MCFragment *F;
602 uint64_t Offset;
603 ElfMappingSymbol State;
604 };
605
Tim Northover5cc3dc82012-12-07 16:50:23 +0000606 void EmitDataMappingSymbol() {
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000607 if (LastEMSInfo->State == EMS_Data)
608 return;
609 else if (LastEMSInfo->State == EMS_None) {
610 // This is a tentative symbol, it won't really be emitted until it's
611 // actually needed.
612 ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
613 auto *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
614 if (!DF)
615 return;
616 EMS->Loc = SMLoc();
617 EMS->F = getCurrentFragment();
618 EMS->Offset = DF->getContents().size();
619 LastEMSInfo->State = EMS_Data;
620 return;
621 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000622 EmitMappingSymbol("$d");
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000623 LastEMSInfo->State = EMS_Data;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000624 }
625
626 void EmitThumbMappingSymbol() {
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000627 if (LastEMSInfo->State == EMS_Thumb)
628 return;
629 FlushPendingMappingSymbol();
Tim Northover5cc3dc82012-12-07 16:50:23 +0000630 EmitMappingSymbol("$t");
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000631 LastEMSInfo->State = EMS_Thumb;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000632 }
633
634 void EmitARMMappingSymbol() {
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000635 if (LastEMSInfo->State == EMS_ARM)
636 return;
637 FlushPendingMappingSymbol();
Tim Northover5cc3dc82012-12-07 16:50:23 +0000638 EmitMappingSymbol("$a");
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000639 LastEMSInfo->State = EMS_ARM;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000640 }
641
642 void EmitMappingSymbol(StringRef Name) {
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000643 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
644 Name + "." + Twine(MappingSymbolCounter++)));
Rafael Espindolac5fb5082015-06-26 11:31:13 +0000645 EmitLabel(Symbol);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000646
Rafael Espindola95fb9b92015-06-02 20:38:46 +0000647 Symbol->setType(ELF::STT_NOTYPE);
648 Symbol->setBinding(ELF::STB_LOCAL);
Rafael Espindola4d37b2a2015-05-29 21:45:01 +0000649 Symbol->setExternal(false);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000650 }
651
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000652 void EmitMappingSymbol(StringRef Name, SMLoc Loc, MCFragment *F,
653 uint64_t Offset) {
654 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
655 Name + "." + Twine(MappingSymbolCounter++)));
656 EmitLabel(Symbol, Loc, F);
657 Symbol->setType(ELF::STT_NOTYPE);
658 Symbol->setBinding(ELF::STB_LOCAL);
659 Symbol->setExternal(false);
660 Symbol->setOffset(Offset);
661 }
662
Craig Topperca7e3e52014-03-10 03:19:03 +0000663 void EmitThumbFunc(MCSymbol *Func) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000664 getAssembler().setIsThumbFunc(Func);
Rafael Espindolab60c8292014-04-29 12:46:50 +0000665 EmitSymbolAttribute(Func, MCSA_ELF_TypeFunction);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000666 }
667
Logan Chien2bcc42c2013-01-30 15:39:04 +0000668 // Helper functions for ARM exception handling directives
Keno Fischer00021422016-01-12 13:38:15 +0000669 void EHReset();
670
671 // Reset state between object emissions
672 void reset() override;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000673
674 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000675 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000676 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000677
Mehdi Amini68c6c8c2016-10-07 08:48:07 +0000678 void SwitchToEHSection(StringRef Prefix, unsigned Type, unsigned Flags,
Logan Chien2bcc42c2013-01-30 15:39:04 +0000679 SectionKind Kind, const MCSymbol &Fn);
680 void SwitchToExTabSection(const MCSymbol &FnStart);
681 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000682
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000683 void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
684
Tim Northover5cc3dc82012-12-07 16:50:23 +0000685 bool IsThumb;
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000686 int64_t MappingSymbolCounter = 0;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000687
Weiming Zhao74a7fa02017-04-03 21:50:04 +0000688 DenseMap<const MCSection *, std::unique_ptr<ElfMappingSymbolInfo>>
689 LastMappingSymbols;
690
691 std::unique_ptr<ElfMappingSymbolInfo> LastEMSInfo;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000692
Logan Chien2bcc42c2013-01-30 15:39:04 +0000693 // ARM Exception Handling Frame Information
694 MCSymbol *ExTab;
695 MCSymbol *FnStart;
696 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000697 unsigned PersonalityIndex;
698 unsigned FPReg; // Frame pointer register
699 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
700 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
701 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000702 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000703 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000704 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000705 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000706};
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000707
Logan Chiend8bb4b72013-04-16 12:02:21 +0000708} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000709
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000710ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000711 return static_cast<ARMELFStreamer &>(Streamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000712}
713
714void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
715void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
716void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000717
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000718void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
719 getStreamer().emitPersonality(Personality);
720}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000721
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000722void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
723 getStreamer().emitPersonalityIndex(Index);
724}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000725
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000726void ARMTargetELFStreamer::emitHandlerData() {
727 getStreamer().emitHandlerData();
728}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000729
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000730void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
731 int64_t Offset) {
732 getStreamer().emitSetFP(FpReg, SpReg, Offset);
733}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000734
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000735void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
736 getStreamer().emitMovSP(Reg, Offset);
737}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000738
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000739void ARMTargetELFStreamer::emitPad(int64_t Offset) {
740 getStreamer().emitPad(Offset);
741}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000742
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000743void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
744 bool isVector) {
745 getStreamer().emitRegSave(RegList, isVector);
746}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000747
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000748void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
749 const SmallVectorImpl<uint8_t> &Opcodes) {
750 getStreamer().emitUnwindRaw(Offset, Opcodes);
751}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000752
Logan Chien8cbb80d2013-10-28 17:51:12 +0000753void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
754 assert(!Vendor.empty() && "Vendor cannot be empty.");
755
756 if (CurrentVendor == Vendor)
757 return;
758
759 if (!CurrentVendor.empty())
760 finishAttributeSection();
761
762 assert(Contents.empty() &&
763 ".ARM.attributes should be flushed before changing vendor");
764 CurrentVendor = Vendor;
765
766}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000767
Logan Chien8cbb80d2013-10-28 17:51:12 +0000768void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
769 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
770}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000771
Logan Chien8cbb80d2013-10-28 17:51:12 +0000772void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
773 StringRef Value) {
774 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
775}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000776
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000777void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
778 unsigned IntValue,
779 StringRef StringValue) {
780 setAttributeItems(Attribute, IntValue, StringValue,
781 /* OverwriteExisting= */ true);
782}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000783
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000784void ARMTargetELFStreamer::emitArch(ARM::ArchKind Value) {
Logan Chien439e8f92013-12-11 17:16:25 +0000785 Arch = Value;
786}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000787
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000788void ARMTargetELFStreamer::emitObjectArch(ARM::ArchKind Value) {
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000789 EmittedArch = Value;
790}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000791
Logan Chien439e8f92013-12-11 17:16:25 +0000792void ARMTargetELFStreamer::emitArchDefaultAttributes() {
793 using namespace ARMBuildAttrs;
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000794
Renato Golinf5f373f2015-05-08 21:04:27 +0000795 setAttributeItem(CPU_name,
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000796 ARM::getCPUAttr(Arch),
Renato Golinf5f373f2015-05-08 21:04:27 +0000797 false);
798
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000799 if (EmittedArch == ARM::ArchKind::INVALID)
Renato Golinf5f373f2015-05-08 21:04:27 +0000800 setAttributeItem(CPU_arch,
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000801 ARM::getArchAttr(Arch),
Renato Golinf5f373f2015-05-08 21:04:27 +0000802 false);
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000803 else
Renato Golinf5f373f2015-05-08 21:04:27 +0000804 setAttributeItem(CPU_arch,
Chandler Carruthbb47b9a2015-08-30 02:09:48 +0000805 ARM::getArchAttr(EmittedArch),
Renato Golinf5f373f2015-05-08 21:04:27 +0000806 false);
Logan Chien439e8f92013-12-11 17:16:25 +0000807
808 switch (Arch) {
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000809 case ARM::ArchKind::ARMV2:
810 case ARM::ArchKind::ARMV2A:
811 case ARM::ArchKind::ARMV3:
812 case ARM::ArchKind::ARMV3M:
813 case ARM::ArchKind::ARMV4:
Logan Chien439e8f92013-12-11 17:16:25 +0000814 setAttributeItem(ARM_ISA_use, Allowed, false);
815 break;
816
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000817 case ARM::ArchKind::ARMV4T:
818 case ARM::ArchKind::ARMV5T:
819 case ARM::ArchKind::ARMV5TE:
820 case ARM::ArchKind::ARMV6:
Logan Chien439e8f92013-12-11 17:16:25 +0000821 setAttributeItem(ARM_ISA_use, Allowed, false);
822 setAttributeItem(THUMB_ISA_use, Allowed, false);
823 break;
824
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000825 case ARM::ArchKind::ARMV6T2:
Logan Chien439e8f92013-12-11 17:16:25 +0000826 setAttributeItem(ARM_ISA_use, Allowed, false);
827 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
828 break;
829
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000830 case ARM::ArchKind::ARMV6K:
831 case ARM::ArchKind::ARMV6KZ:
Logan Chien439e8f92013-12-11 17:16:25 +0000832 setAttributeItem(ARM_ISA_use, Allowed, false);
833 setAttributeItem(THUMB_ISA_use, Allowed, false);
834 setAttributeItem(Virtualization_use, AllowTZ, false);
835 break;
836
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000837 case ARM::ArchKind::ARMV6M:
Logan Chien439e8f92013-12-11 17:16:25 +0000838 setAttributeItem(THUMB_ISA_use, Allowed, false);
839 break;
840
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000841 case ARM::ArchKind::ARMV7A:
Logan Chien439e8f92013-12-11 17:16:25 +0000842 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
843 setAttributeItem(ARM_ISA_use, Allowed, false);
844 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
845 break;
846
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000847 case ARM::ArchKind::ARMV7R:
Logan Chien439e8f92013-12-11 17:16:25 +0000848 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
849 setAttributeItem(ARM_ISA_use, Allowed, false);
850 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
851 break;
852
Oliver Stannard9ea2eae2017-11-29 10:12:15 +0000853 case ARM::ArchKind::ARMV7EM:
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000854 case ARM::ArchKind::ARMV7M:
Logan Chien439e8f92013-12-11 17:16:25 +0000855 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
856 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
857 break;
858
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000859 case ARM::ArchKind::ARMV8A:
860 case ARM::ArchKind::ARMV8_1A:
861 case ARM::ArchKind::ARMV8_2A:
Logan Chien439e8f92013-12-11 17:16:25 +0000862 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
863 setAttributeItem(ARM_ISA_use, Allowed, false);
864 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
865 setAttributeItem(MPextension_use, Allowed, false);
866 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
867 break;
868
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000869 case ARM::ArchKind::ARMV8MBaseline:
870 case ARM::ArchKind::ARMV8MMainline:
Bradley Smithe26f7992016-01-15 10:24:39 +0000871 setAttributeItem(THUMB_ISA_use, AllowThumbDerived, false);
872 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
873 break;
874
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000875 case ARM::ArchKind::IWMMXT:
Logan Chien439e8f92013-12-11 17:16:25 +0000876 setAttributeItem(ARM_ISA_use, Allowed, false);
877 setAttributeItem(THUMB_ISA_use, Allowed, false);
878 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
879 break;
880
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000881 case ARM::ArchKind::IWMMXT2:
Logan Chien439e8f92013-12-11 17:16:25 +0000882 setAttributeItem(ARM_ISA_use, Allowed, false);
883 setAttributeItem(THUMB_ISA_use, Allowed, false);
884 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
885 break;
886
887 default:
Florian Hahn67ddd1d2017-07-27 16:27:56 +0000888 report_fatal_error("Unknown Arch: " + Twine(ARM::getArchName(Arch)));
Logan Chien439e8f92013-12-11 17:16:25 +0000889 break;
890 }
891}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000892
Logan Chien8cbb80d2013-10-28 17:51:12 +0000893void ARMTargetELFStreamer::emitFPU(unsigned Value) {
894 FPU = Value;
895}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +0000896
Logan Chien8cbb80d2013-10-28 17:51:12 +0000897void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
898 switch (FPU) {
Renato Golin35de35d2015-05-12 10:33:58 +0000899 case ARM::FK_VFP:
900 case ARM::FK_VFPV2:
Logan Chiena39510a2013-12-18 17:23:15 +0000901 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000902 ARMBuildAttrs::AllowFPv2,
903 /* OverwriteExisting= */ false);
904 break;
905
Renato Golin35de35d2015-05-12 10:33:58 +0000906 case ARM::FK_VFPV3:
Logan Chiena39510a2013-12-18 17:23:15 +0000907 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000908 ARMBuildAttrs::AllowFPv3A,
909 /* OverwriteExisting= */ false);
910 break;
911
Javed Absard5526302015-06-29 09:32:29 +0000912 case ARM::FK_VFPV3_FP16:
913 setAttributeItem(ARMBuildAttrs::FP_arch,
914 ARMBuildAttrs::AllowFPv3A,
915 /* OverwriteExisting= */ false);
916 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
917 ARMBuildAttrs::AllowHPFP,
918 /* OverwriteExisting= */ false);
919 break;
920
Renato Golin35de35d2015-05-12 10:33:58 +0000921 case ARM::FK_VFPV3_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000922 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000923 ARMBuildAttrs::AllowFPv3B,
924 /* OverwriteExisting= */ false);
925 break;
926
Javed Absard5526302015-06-29 09:32:29 +0000927 case ARM::FK_VFPV3_D16_FP16:
928 setAttributeItem(ARMBuildAttrs::FP_arch,
929 ARMBuildAttrs::AllowFPv3B,
930 /* OverwriteExisting= */ false);
931 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
932 ARMBuildAttrs::AllowHPFP,
933 /* OverwriteExisting= */ false);
934 break;
935
936 case ARM::FK_VFPV3XD:
937 setAttributeItem(ARMBuildAttrs::FP_arch,
938 ARMBuildAttrs::AllowFPv3B,
939 /* OverwriteExisting= */ false);
940 break;
941 case ARM::FK_VFPV3XD_FP16:
942 setAttributeItem(ARMBuildAttrs::FP_arch,
943 ARMBuildAttrs::AllowFPv3B,
944 /* OverwriteExisting= */ false);
945 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
946 ARMBuildAttrs::AllowHPFP,
947 /* OverwriteExisting= */ false);
948 break;
949
Renato Golin35de35d2015-05-12 10:33:58 +0000950 case ARM::FK_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000951 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000952 ARMBuildAttrs::AllowFPv4A,
953 /* OverwriteExisting= */ false);
954 break;
955
John Brawn985c04e2015-06-05 13:31:19 +0000956 // ABI_HardFP_use is handled in ARMAsmPrinter, so _SP_D16 is treated the same
957 // as _D16 here.
958 case ARM::FK_FPV4_SP_D16:
Renato Golin35de35d2015-05-12 10:33:58 +0000959 case ARM::FK_VFPV4_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000960 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000961 ARMBuildAttrs::AllowFPv4B,
962 /* OverwriteExisting= */ false);
963 break;
964
Renato Golin35de35d2015-05-12 10:33:58 +0000965 case ARM::FK_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000966 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000967 ARMBuildAttrs::AllowFPARMv8A,
968 /* OverwriteExisting= */ false);
969 break;
970
Oliver Stannard37e4daa2014-10-01 09:02:17 +0000971 // FPV5_D16 is identical to FP_ARMV8 except for the number of D registers, so
972 // uses the FP_ARMV8_D16 build attribute.
John Brawn985c04e2015-06-05 13:31:19 +0000973 case ARM::FK_FPV5_SP_D16:
Renato Golin35de35d2015-05-12 10:33:58 +0000974 case ARM::FK_FPV5_D16:
Oliver Stannard37e4daa2014-10-01 09:02:17 +0000975 setAttributeItem(ARMBuildAttrs::FP_arch,
976 ARMBuildAttrs::AllowFPARMv8B,
977 /* OverwriteExisting= */ false);
978 break;
979
Renato Golin35de35d2015-05-12 10:33:58 +0000980 case ARM::FK_NEON:
Logan Chiena39510a2013-12-18 17:23:15 +0000981 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000982 ARMBuildAttrs::AllowFPv3A,
983 /* OverwriteExisting= */ false);
984 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
985 ARMBuildAttrs::AllowNeon,
986 /* OverwriteExisting= */ false);
987 break;
988
Javed Absard5526302015-06-29 09:32:29 +0000989 case ARM::FK_NEON_FP16:
990 setAttributeItem(ARMBuildAttrs::FP_arch,
991 ARMBuildAttrs::AllowFPv3A,
992 /* OverwriteExisting= */ false);
993 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
994 ARMBuildAttrs::AllowNeon,
995 /* OverwriteExisting= */ false);
996 setAttributeItem(ARMBuildAttrs::FP_HP_extension,
997 ARMBuildAttrs::AllowHPFP,
998 /* OverwriteExisting= */ false);
999 break;
1000
Renato Golin35de35d2015-05-12 10:33:58 +00001001 case ARM::FK_NEON_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +00001002 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +00001003 ARMBuildAttrs::AllowFPv4A,
1004 /* OverwriteExisting= */ false);
1005 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
1006 ARMBuildAttrs::AllowNeon2,
1007 /* OverwriteExisting= */ false);
1008 break;
1009
Renato Golin35de35d2015-05-12 10:33:58 +00001010 case ARM::FK_NEON_FP_ARMV8:
1011 case ARM::FK_CRYPTO_NEON_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +00001012 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +00001013 ARMBuildAttrs::AllowFPARMv8A,
1014 /* OverwriteExisting= */ false);
Vladimir Sukharevc632cda2015-03-26 17:05:54 +00001015 // 'Advanced_SIMD_arch' must be emitted not here, but within
1016 // ARMAsmPrinter::emitAttributes(), depending on hasV8Ops() and hasV8_1a()
Logan Chien8cbb80d2013-10-28 17:51:12 +00001017 break;
1018
Renato Golin35de35d2015-05-12 10:33:58 +00001019 case ARM::FK_SOFTVFP:
John Brawn985c04e2015-06-05 13:31:19 +00001020 case ARM::FK_NONE:
Logan Chien05ae7442014-01-02 15:50:02 +00001021 break;
1022
Logan Chien8cbb80d2013-10-28 17:51:12 +00001023 default:
1024 report_fatal_error("Unknown FPU: " + Twine(FPU));
1025 break;
1026 }
1027}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +00001028
Logan Chien8cbb80d2013-10-28 17:51:12 +00001029size_t ARMTargetELFStreamer::calculateContentSize() const {
1030 size_t Result = 0;
1031 for (size_t i = 0; i < Contents.size(); ++i) {
1032 AttributeItem item = Contents[i];
1033 switch (item.Type) {
1034 case AttributeItem::HiddenAttribute:
1035 break;
1036 case AttributeItem::NumericAttribute:
Logan Chien5b776b72014-02-22 14:00:39 +00001037 Result += getULEB128Size(item.Tag);
1038 Result += getULEB128Size(item.IntValue);
Logan Chien8cbb80d2013-10-28 17:51:12 +00001039 break;
1040 case AttributeItem::TextAttribute:
Logan Chien5b776b72014-02-22 14:00:39 +00001041 Result += getULEB128Size(item.Tag);
Logan Chien8cbb80d2013-10-28 17:51:12 +00001042 Result += item.StringValue.size() + 1; // string + '\0'
1043 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +00001044 case AttributeItem::NumericAndTextAttributes:
Logan Chien5b776b72014-02-22 14:00:39 +00001045 Result += getULEB128Size(item.Tag);
1046 Result += getULEB128Size(item.IntValue);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +00001047 Result += item.StringValue.size() + 1; // string + '\0';
1048 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +00001049 }
1050 }
1051 return Result;
1052}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +00001053
Logan Chien8cbb80d2013-10-28 17:51:12 +00001054void ARMTargetELFStreamer::finishAttributeSection() {
1055 // <format-version>
1056 // [ <section-length> "vendor-name"
1057 // [ <file-tag> <size> <attribute>*
1058 // | <section-tag> <size> <section-number>* 0 <attribute>*
1059 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
1060 // ]+
1061 // ]*
1062
Renato Golin35de35d2015-05-12 10:33:58 +00001063 if (FPU != ARM::FK_INVALID)
Logan Chien8cbb80d2013-10-28 17:51:12 +00001064 emitFPUDefaultAttributes();
1065
Florian Hahn67ddd1d2017-07-27 16:27:56 +00001066 if (Arch != ARM::ArchKind::INVALID)
Logan Chien439e8f92013-12-11 17:16:25 +00001067 emitArchDefaultAttributes();
1068
Logan Chien8cbb80d2013-10-28 17:51:12 +00001069 if (Contents.empty())
1070 return;
1071
Mandeep Singh Grang9893fe22018-04-05 18:31:50 +00001072 llvm::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
Logan Chien8cbb80d2013-10-28 17:51:12 +00001073
1074 ARMELFStreamer &Streamer = getStreamer();
1075
1076 // Switch to .ARM.attributes section
1077 if (AttributeSection) {
1078 Streamer.SwitchSection(AttributeSection);
1079 } else {
Rafael Espindolaba31e272015-01-29 17:33:21 +00001080 AttributeSection = Streamer.getContext().getELFSection(
1081 ".ARM.attributes", ELF::SHT_ARM_ATTRIBUTES, 0);
Logan Chien8cbb80d2013-10-28 17:51:12 +00001082 Streamer.SwitchSection(AttributeSection);
1083
1084 // Format version
1085 Streamer.EmitIntValue(0x41, 1);
1086 }
1087
1088 // Vendor size + Vendor name + '\0'
1089 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
1090
1091 // Tag + Tag Size
1092 const size_t TagHeaderSize = 1 + 4;
1093
1094 const size_t ContentsSize = calculateContentSize();
1095
1096 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
1097 Streamer.EmitBytes(CurrentVendor);
1098 Streamer.EmitIntValue(0, 1); // '\0'
1099
1100 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
1101 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
1102
1103 // Size should have been accounted for already, now
1104 // emit each field as its type (ULEB or String)
1105 for (size_t i = 0; i < Contents.size(); ++i) {
1106 AttributeItem item = Contents[i];
1107 Streamer.EmitULEB128IntValue(item.Tag);
1108 switch (item.Type) {
1109 default: llvm_unreachable("Invalid attribute type");
1110 case AttributeItem::NumericAttribute:
1111 Streamer.EmitULEB128IntValue(item.IntValue);
1112 break;
1113 case AttributeItem::TextAttribute:
Charlie Turner8d433692014-11-27 12:13:56 +00001114 Streamer.EmitBytes(item.StringValue);
Logan Chien8cbb80d2013-10-28 17:51:12 +00001115 Streamer.EmitIntValue(0, 1); // '\0'
1116 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +00001117 case AttributeItem::NumericAndTextAttributes:
1118 Streamer.EmitULEB128IntValue(item.IntValue);
Charlie Turner8d433692014-11-27 12:13:56 +00001119 Streamer.EmitBytes(item.StringValue);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +00001120 Streamer.EmitIntValue(0, 1); // '\0'
1121 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +00001122 }
1123 }
1124
1125 Contents.clear();
Renato Golin35de35d2015-05-12 10:33:58 +00001126 FPU = ARM::FK_INVALID;
Logan Chien8cbb80d2013-10-28 17:51:12 +00001127}
Rafael Espindola4c6f6132014-04-27 17:10:46 +00001128
1129void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
1130 ARMELFStreamer &Streamer = getStreamer();
1131 if (!Streamer.IsThumb)
1132 return;
1133
Rafael Espindolac73aed12015-06-03 19:03:11 +00001134 Streamer.getAssembler().registerSymbol(*Symbol);
Rafael Espindola95fb9b92015-06-02 20:38:46 +00001135 unsigned Type = cast<MCSymbolELF>(Symbol)->getType();
Rafael Espindolaf8794ff2015-06-04 00:47:43 +00001136 if (Type == ELF::STT_FUNC || Type == ELF::STT_GNU_IFUNC)
Rafael Espindola4c6f6132014-04-27 17:10:46 +00001137 Streamer.EmitThumbFunc(Symbol);
1138}
1139
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001140void
1141ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
1142 getStreamer().EmitFixup(S, FK_Data_4);
1143}
Rafael Espindola466d6632014-04-27 20:23:58 +00001144
1145void ARMTargetELFStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
1146 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
Rafael Espindola4a042942014-05-01 12:45:43 +00001147 const MCSymbol &Sym = SRE->getSymbol();
1148 if (!Sym.isDefined()) {
Rafael Espindola466d6632014-04-27 20:23:58 +00001149 getStreamer().EmitAssignment(Symbol, Value);
1150 return;
1151 }
1152 }
1153
1154 getStreamer().EmitThumbFunc(Symbol);
1155 getStreamer().EmitAssignment(Symbol, Value);
1156}
1157
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +00001158void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
1159 getStreamer().emitInst(Inst, Suffix);
1160}
Logan Chien8cbb80d2013-10-28 17:51:12 +00001161
Keno Fischer00021422016-01-12 13:38:15 +00001162void ARMTargetELFStreamer::reset() { AttributeSection = nullptr; }
1163
Logan Chien8cbb80d2013-10-28 17:51:12 +00001164void ARMELFStreamer::FinishImpl() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +00001165 MCTargetStreamer &TS = *getTargetStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +00001166 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1167 ATS.finishAttributeSection();
1168
1169 MCELFStreamer::FinishImpl();
1170}
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001171
Keno Fischer00021422016-01-12 13:38:15 +00001172void ARMELFStreamer::reset() {
1173 MCTargetStreamer &TS = *getTargetStreamer();
1174 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1175 ATS.reset();
1176 MappingSymbolCounter = 0;
1177 MCELFStreamer::reset();
Yichao Yu221dae32017-10-26 17:36:43 +00001178 LastMappingSymbols.clear();
1179 LastEMSInfo.reset();
Keno Fischer00021422016-01-12 13:38:15 +00001180 // MCELFStreamer clear's the assembler's e_flags. However, for
1181 // arm we manually set the ABI version on streamer creation, so
1182 // do the same here
1183 getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1184}
1185
Mehdi Amini68c6c8c2016-10-07 08:48:07 +00001186inline void ARMELFStreamer::SwitchToEHSection(StringRef Prefix,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001187 unsigned Type,
1188 unsigned Flags,
1189 SectionKind Kind,
1190 const MCSymbol &Fn) {
1191 const MCSectionELF &FnSection =
1192 static_cast<const MCSectionELF &>(Fn.getSection());
1193
1194 // Create the name for new section
1195 StringRef FnSecName(FnSection.getSectionName());
1196 SmallString<128> EHSecName(Prefix);
1197 if (FnSecName != ".text") {
1198 EHSecName += FnSecName;
1199 }
1200
1201 // Get .ARM.extab or .ARM.exidx section
Rafael Espindola0ccf9b72015-06-02 21:30:13 +00001202 const MCSymbolELF *Group = FnSection.getGroup();
Rafael Espindola61e8ce32015-04-06 04:25:18 +00001203 if (Group)
1204 Flags |= ELF::SHF_GROUP;
Rafael Espindola13a79bb2017-02-02 21:26:06 +00001205 MCSectionELF *EHSection = getContext().getELFSection(
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +00001206 EHSecName, Type, Flags, 0, Group, FnSection.getUniqueID(),
1207 static_cast<const MCSymbolELF *>(&Fn));
Rafael Espindola61e8ce32015-04-06 04:25:18 +00001208
Logan Chiend8bb4b72013-04-16 12:02:21 +00001209 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001210
1211 // Switch to .ARM.extab or .ARM.exidx section
1212 SwitchSection(EHSection);
Rafael Espindola7b514962014-02-04 18:34:04 +00001213 EmitCodeAlignment(4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001214}
1215
1216inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
Rafael Espindola449711c2015-11-18 06:02:15 +00001217 SwitchToEHSection(".ARM.extab", ELF::SHT_PROGBITS, ELF::SHF_ALLOC,
1218 SectionKind::getData(), FnStart);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001219}
1220
1221inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
Rafael Espindola449711c2015-11-18 06:02:15 +00001222 SwitchToEHSection(".ARM.exidx", ELF::SHT_ARM_EXIDX,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001223 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
Rafael Espindola449711c2015-11-18 06:02:15 +00001224 SectionKind::getData(), FnStart);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001225}
Eugene Zelenkoe79c0772017-01-27 23:58:02 +00001226
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001227void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1228 MCDataFragment *Frag = getOrCreateDataFragment();
Jim Grosbach63661f82015-05-15 19:13:05 +00001229 Frag->getFixups().push_back(MCFixup::create(Frag->getContents().size(), Expr,
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001230 Kind));
1231}
Logan Chien2bcc42c2013-01-30 15:39:04 +00001232
Keno Fischer00021422016-01-12 13:38:15 +00001233void ARMELFStreamer::EHReset() {
Craig Topper062a2ba2014-04-25 05:30:21 +00001234 ExTab = nullptr;
1235 FnStart = nullptr;
1236 Personality = nullptr;
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001237 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
Logan Chien325823a2013-06-09 12:22:30 +00001238 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001239 FPOffset = 0;
1240 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +00001241 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001242 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001243 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001244
Logan Chien325823a2013-06-09 12:22:30 +00001245 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001246 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001247}
1248
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001249void ARMELFStreamer::emitFnStart() {
Craig Toppere73658d2014-04-28 04:05:08 +00001250 assert(FnStart == nullptr);
Jim Grosbach6f482002015-05-18 18:43:14 +00001251 FnStart = getContext().createTempSymbol();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001252 EmitLabel(FnStart);
1253}
1254
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001255void ARMELFStreamer::emitFnEnd() {
Alp Tokercb402912014-01-24 17:20:08 +00001256 assert(FnStart && ".fnstart must precedes .fnend");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001257
1258 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +00001259 if (!ExTab && !CantUnwind)
1260 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001261
1262 // Emit the exception index table entry
1263 SwitchToExIdxSection(*FnStart);
1264
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001265 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
Logan Chiend8bb4b72013-04-16 12:02:21 +00001266 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001267
1268 const MCSymbolRefExpr *FnStartRef =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001269 MCSymbolRefExpr::create(FnStart,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001270 MCSymbolRefExpr::VK_ARM_PREL31,
1271 getContext());
1272
Rafael Espindola64e1af82013-07-02 15:49:13 +00001273 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001274
1275 if (CantUnwind) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001276 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001277 } else if (ExTab) {
1278 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001279 const MCSymbolRefExpr *ExTabEntryRef =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001280 MCSymbolRefExpr::create(ExTab,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001281 MCSymbolRefExpr::VK_ARM_PREL31,
1282 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +00001283 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001284 } else {
1285 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1286 // the second word of exception index table entry. The size of the unwind
1287 // opcodes should always be 4 bytes.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001288 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
Jonathan Roelofs4971b402014-05-15 02:24:50 +00001289 "Compact model must use __aeabi_unwind_cpp_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +00001290 assert(Opcodes.size() == 4u &&
Jonathan Roelofs4971b402014-05-15 02:24:50 +00001291 "Unwind opcode size for __aeabi_unwind_cpp_pr0 must be equal to 4");
Christian Pirker39db7ec2014-05-13 16:44:30 +00001292 uint64_t Intval = Opcodes[0] |
1293 Opcodes[1] << 8 |
1294 Opcodes[2] << 16 |
1295 Opcodes[3] << 24;
1296 EmitIntValue(Intval, Opcodes.size());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001297 }
1298
Logan Chien4ea23b52013-05-10 16:17:24 +00001299 // Switch to the section containing FnStart
1300 SwitchSection(&FnStart->getSection());
1301
Logan Chien2bcc42c2013-01-30 15:39:04 +00001302 // Clean exception handling frame information
Keno Fischer00021422016-01-12 13:38:15 +00001303 EHReset();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001304}
1305
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001306void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1307
1308// Add the R_ARM_NONE fixup at the same position
1309void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
Jim Grosbach6f482002015-05-18 18:43:14 +00001310 const MCSymbol *PersonalitySym = getContext().getOrCreateSymbol(Name);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001311
Jim Grosbach13760bd2015-05-30 01:25:56 +00001312 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::create(
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001313 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1314
Rafael Espindola2be12812014-06-25 15:29:54 +00001315 visitUsedExpr(*PersonalityRef);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001316 MCDataFragment *DF = getOrCreateDataFragment();
Jim Grosbach63661f82015-05-15 19:13:05 +00001317 DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001318 PersonalityRef,
1319 MCFixup::getKindForSize(4, false)));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001320}
1321
Logan Chien325823a2013-06-09 12:22:30 +00001322void ARMELFStreamer::FlushPendingOffset() {
1323 if (PendingOffset != 0) {
1324 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1325 PendingOffset = 0;
1326 }
1327}
1328
Logan Chienc931fce2013-07-02 12:43:27 +00001329void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +00001330 // Emit the unwind opcode to restore $sp.
1331 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001332 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +00001333 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1334 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001335 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +00001336 } else {
1337 FlushPendingOffset();
1338 }
1339
1340 // Finalize the unwind opcode sequence
1341 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +00001342
1343 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1344 // section. Thus, we don't have to create an entry in the .ARM.extab
1345 // section.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001346 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +00001347 return;
1348
1349 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001350 SwitchToExTabSection(*FnStart);
1351
1352 // Create .ARM.extab label for offset in .ARM.exidx
1353 assert(!ExTab);
Jim Grosbach6f482002015-05-18 18:43:14 +00001354 ExTab = getContext().createTempSymbol();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001355 EmitLabel(ExTab);
1356
Logan Chien4ea23b52013-05-10 16:17:24 +00001357 // Emit personality
1358 if (Personality) {
1359 const MCSymbolRefExpr *PersonalityRef =
Jim Grosbach13760bd2015-05-30 01:25:56 +00001360 MCSymbolRefExpr::create(Personality,
Logan Chien4ea23b52013-05-10 16:17:24 +00001361 MCSymbolRefExpr::VK_ARM_PREL31,
1362 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001363
Rafael Espindola64e1af82013-07-02 15:49:13 +00001364 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +00001365 }
Logan Chien2bcc42c2013-01-30 15:39:04 +00001366
1367 // Emit unwind opcodes
Christian Pirker39db7ec2014-05-13 16:44:30 +00001368 assert((Opcodes.size() % 4) == 0 &&
1369 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be multiple of 4");
1370 for (unsigned I = 0; I != Opcodes.size(); I += 4) {
1371 uint64_t Intval = Opcodes[I] |
1372 Opcodes[I + 1] << 8 |
1373 Opcodes[I + 2] << 16 |
1374 Opcodes[I + 3] << 24;
1375 EmitIntValue(Intval, 4);
1376 }
Logan Chienc931fce2013-07-02 12:43:27 +00001377
1378 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1379 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1380 // after the unwind opcodes. The handler data consists of several 32-bit
1381 // words, and should be terminated by zero.
1382 //
1383 // In case that the .handlerdata directive is not specified by the
1384 // programmer, we should emit zero to terminate the handler data.
1385 if (NoHandlerData && !Personality)
1386 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001387}
1388
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001389void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
Logan Chien4ea23b52013-05-10 16:17:24 +00001390
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001391void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001392 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001393 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001394}
1395
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +00001396void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1397 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1398 PersonalityIndex = Index;
1399}
1400
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001401void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001402 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001403 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001404 "the operand of .setfp directive should be either $sp or $fp");
1405
1406 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +00001407 FPReg = NewFPReg;
1408
1409 if (NewSPReg == ARM::SP)
1410 FPOffset = SPOffset + Offset;
1411 else
1412 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001413}
1414
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +00001415void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
1416 assert((Reg != ARM::SP && Reg != ARM::PC) &&
1417 "the operand of .movsp cannot be either sp or pc");
1418 assert(FPReg == ARM::SP && "current FP must be SP");
1419
1420 FlushPendingOffset();
1421
1422 FPReg = Reg;
1423 FPOffset = SPOffset + Offset;
1424
1425 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1426 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1427}
1428
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001429void ARMELFStreamer::emitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001430 // Track the change of the $sp offset
1431 SPOffset -= Offset;
1432
1433 // To squash multiple .pad directives, we should delay the unwind opcode
1434 // until the .save, .vsave, .handlerdata, or .fnend directives.
1435 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001436}
1437
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001438void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001439 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +00001440 // Collect the registers in the register list
1441 unsigned Count = 0;
1442 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +00001443 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001444 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001445 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +00001446 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +00001447 unsigned Bit = (1u << Reg);
1448 if ((Mask & Bit) == 0) {
1449 Mask |= Bit;
1450 ++Count;
1451 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001452 }
Logan Chien325823a2013-06-09 12:22:30 +00001453
1454 // Track the change the $sp offset: For the .save directive, the
1455 // corresponding push instruction will decrease the $sp by (4 * Count).
1456 // For the .vsave directive, the corresponding vpush instruction will
1457 // decrease $sp by (8 * Count).
1458 SPOffset -= Count * (IsVector ? 8 : 4);
1459
1460 // Emit the opcode
1461 FlushPendingOffset();
1462 if (IsVector)
1463 UnwindOpAsm.EmitVFPRegSave(Mask);
1464 else
1465 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001466}
1467
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +00001468void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1469 const SmallVectorImpl<uint8_t> &Opcodes) {
1470 FlushPendingOffset();
1471 SPOffset = SPOffset - Offset;
1472 UnwindOpAsm.EmitRaw(Opcodes);
1473}
1474
Tim Northover5cc3dc82012-12-07 16:50:23 +00001475namespace llvm {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001476
Rafael Espindola73870dd2015-03-16 21:43:42 +00001477MCTargetStreamer *createARMTargetAsmStreamer(MCStreamer &S,
1478 formatted_raw_ostream &OS,
1479 MCInstPrinter *InstPrint,
1480 bool isVerboseAsm) {
1481 return new ARMTargetAsmStreamer(S, OS, *InstPrint, isVerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001482}
1483
Peter Collingbourne20c72592015-02-19 00:45:02 +00001484MCTargetStreamer *createARMNullTargetStreamer(MCStreamer &S) {
1485 return new ARMTargetStreamer(S);
1486}
1487
Rafael Espindolacd584a82015-03-19 01:50:16 +00001488MCTargetStreamer *createARMObjectTargetStreamer(MCStreamer &S,
1489 const MCSubtargetInfo &STI) {
Daniel Sanders50f17232015-09-15 16:17:27 +00001490 const Triple &TT = STI.getTargetTriple();
Daniel Sandersfbdab432015-07-06 16:33:18 +00001491 if (TT.isOSBinFormatELF())
Rafael Espindolacd584a82015-03-19 01:50:16 +00001492 return new ARMTargetELFStreamer(S);
1493 return new ARMTargetStreamer(S);
1494}
1495
Lang Hames02d33052017-10-11 01:57:21 +00001496MCELFStreamer *createARMELFStreamer(MCContext &Context,
1497 std::unique_ptr<MCAsmBackend> TAB,
Peter Collingbournef7b81db2018-05-18 18:26:45 +00001498 std::unique_ptr<MCObjectWriter> OW,
Lang Hames2241ffa2017-10-11 23:34:47 +00001499 std::unique_ptr<MCCodeEmitter> Emitter,
1500 bool RelaxAll, bool IsThumb) {
Peter Collingbournef7b81db2018-05-18 18:26:45 +00001501 ARMELFStreamer *S = new ARMELFStreamer(Context, std::move(TAB), std::move(OW),
Lang Hames2241ffa2017-10-11 23:34:47 +00001502 std::move(Emitter), IsThumb);
Lang Hames02d33052017-10-11 01:57:21 +00001503 // FIXME: This should eventually end up somewhere else where more
1504 // intelligent flag decisions can be made. For now we are just maintaining
1505 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1506 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001507
Lang Hames02d33052017-10-11 01:57:21 +00001508 if (RelaxAll)
1509 S->getAssembler().setRelaxAll(true);
1510 return S;
Alexander Kornienkof00654e2015-06-23 09:49:53 +00001511}
Tim Northover5cc3dc82012-12-07 16:50:23 +00001512
Eugene Zelenkoe79c0772017-01-27 23:58:02 +00001513} // end namespace llvm