Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- lib/MC/ARMELFStreamer.cpp - ELF Object Output for ARM --------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file assembles .s files and emits ARM ELF .o object files. Different
10
// from generic ELF streamer in emitting mapping symbols ($a, $t and $d) to
11
// delimit regions of data and code.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "ARMRegisterInfo.h"
16
#include "ARMUnwindOpAsm.h"
17
#include "llvm/ADT/DenseMap.h"
18
#include "llvm/ADT/SmallString.h"
19
#include "llvm/ADT/SmallVector.h"
20
#include "llvm/ADT/StringRef.h"
21
#include "llvm/ADT/Triple.h"
22
#include "llvm/ADT/Twine.h"
23
#include "llvm/BinaryFormat/ELF.h"
24
#include "llvm/MC/MCAsmBackend.h"
25
#include "llvm/MC/MCAsmInfo.h"
26
#include "llvm/MC/MCAssembler.h"
27
#include "llvm/MC/MCCodeEmitter.h"
28
#include "llvm/MC/MCContext.h"
29
#include "llvm/MC/MCELFStreamer.h"
30
#include "llvm/MC/MCExpr.h"
31
#include "llvm/MC/MCFixup.h"
32
#include "llvm/MC/MCFragment.h"
33
#include "llvm/MC/MCInst.h"
34
#include "llvm/MC/MCInstPrinter.h"
35
#include "llvm/MC/MCObjectWriter.h"
36
#include "llvm/MC/MCRegisterInfo.h"
37
#include "llvm/MC/MCSection.h"
38
#include "llvm/MC/MCSectionELF.h"
39
#include "llvm/MC/MCStreamer.h"
40
#include "llvm/MC/MCSubtargetInfo.h"
41
#include "llvm/MC/MCSymbol.h"
42
#include "llvm/MC/MCSymbolELF.h"
43
#include "llvm/MC/SectionKind.h"
44
#include "llvm/Support/ARMBuildAttributes.h"
45
#include "llvm/Support/ARMEHABI.h"
46
#include "llvm/Support/Casting.h"
47
#include "llvm/Support/ErrorHandling.h"
48
#include "llvm/Support/FormattedStream.h"
49
#include "llvm/Support/LEB128.h"
50
#include "llvm/Support/TargetParser.h"
51
#include "llvm/Support/raw_ostream.h"
52
#include <algorithm>
53
#include <cassert>
54
#include <climits>
55
#include <cstddef>
56
#include <cstdint>
57
#include <string>
58
59
using namespace llvm;
60
61
457
static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
62
457
  assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX &&
63
457
         "Invalid personality index");
64
457
  return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
65
457
}
66
67
namespace {
68
69
class ARMELFStreamer;
70
71
class ARMTargetAsmStreamer : public ARMTargetStreamer {
72
  formatted_raw_ostream &OS;
73
  MCInstPrinter &InstPrinter;
74
  bool IsVerboseAsm;
75
76
  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;
89
90
  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,
94
                            StringRef StringValue) override;
95
  void emitArch(ARM::ArchKind Arch) override;
96
  void emitArchExtension(unsigned ArchExt) override;
97
  void emitObjectArch(ARM::ArchKind Arch) override;
98
  void emitFPU(unsigned FPU) override;
99
  void emitInst(uint32_t Inst, char Suffix = '\0') override;
100
  void finishAttributeSection() override;
101
102
  void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
103
  void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
104
105
public:
106
  ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
107
                       MCInstPrinter &InstPrinter, bool VerboseAsm);
108
};
109
110
ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
111
                                           formatted_raw_ostream &OS,
112
                                           MCInstPrinter &InstPrinter,
113
                                           bool VerboseAsm)
114
    : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
115
3.83k
      IsVerboseAsm(VerboseAsm) {}
116
117
13.7k
void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
118
13.7k
void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
119
4.32k
void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
120
121
35
void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
122
35
  OS << "\t.personality " << Personality->getName() << '\n';
123
35
}
124
125
3
void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
126
3
  OS << "\t.personalityindex " << Index << '\n';
127
3
}
128
129
39
void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
130
131
void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
132
404
                                     int64_t Offset) {
133
404
  OS << "\t.setfp\t";
134
404
  InstPrinter.printRegName(OS, FpReg);
135
404
  OS << ", ";
136
404
  InstPrinter.printRegName(OS, SpReg);
137
404
  if (Offset)
138
200
    OS << ", #" << Offset;
139
404
  OS << '\n';
140
404
}
141
142
2
void ARMTargetAsmStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
143
2
  assert((Reg != ARM::SP && Reg != ARM::PC) &&
144
2
         "the operand of .movsp cannot be either sp or pc");
145
2
146
2
  OS << "\t.movsp\t";
147
2
  InstPrinter.printRegName(OS, Reg);
148
2
  if (Offset)
149
0
    OS << ", #" << Offset;
150
2
  OS << '\n';
151
2
}
152
153
780
void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
154
780
  OS << "\t.pad\t#" << Offset << '\n';
155
780
}
156
157
void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
158
2.86k
                                       bool isVector) {
159
2.86k
  assert(RegList.size() && "RegList should not be empty");
160
2.86k
  if (isVector)
161
242
    OS << "\t.vsave\t{";
162
2.61k
  else
163
2.61k
    OS << "\t.save\t{";
164
2.86k
165
2.86k
  InstPrinter.printRegName(OS, RegList[0]);
166
2.86k
167
7.73k
  for (unsigned i = 1, e = RegList.size(); i != e; 
++i4.87k
) {
168
4.87k
    OS << ", ";
169
4.87k
    InstPrinter.printRegName(OS, RegList[i]);
170
4.87k
  }
171
2.86k
172
2.86k
  OS << "}\n";
173
2.86k
}
174
175
4.21k
void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {}
176
177
30.1k
void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
178
30.1k
  OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
179
30.1k
  if (IsVerboseAsm) {
180
29.4k
    StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
181
29.4k
    if (!Name.empty())
182
29.4k
      OS << "\t@ " << Name;
183
29.4k
  }
184
30.1k
  OS << "\n";
185
30.1k
}
186
187
void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
188
2.67k
                                             StringRef String) {
189
2.67k
  switch (Attribute) {
190
2.67k
  case ARMBuildAttrs::CPU_name:
191
555
    OS << "\t.cpu\t" << String.lower();
192
555
    break;
193
2.67k
  default:
194
2.11k
    OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
195
2.11k
    if (IsVerboseAsm) {
196
2.07k
      StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
197
2.07k
      if (!Name.empty())
198
2.07k
        OS << "\t@ " << Name;
199
2.07k
    }
200
2.11k
    break;
201
2.67k
  }
202
2.67k
  OS << "\n";
203
2.67k
}
204
205
void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
206
                                                unsigned IntValue,
207
6
                                                StringRef StringValue) {
208
6
  switch (Attribute) {
209
6
  
default: 0
llvm_unreachable0
("unsupported multi-value attribute in asm mode");
210
6
  case ARMBuildAttrs::compatibility:
211
6
    OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
212
6
    if (!StringValue.empty())
213
4
      OS << ", \"" << StringValue << "\"";
214
6
    if (IsVerboseAsm)
215
6
      OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
216
6
    break;
217
6
  }
218
6
  OS << "\n";
219
6
}
220
221
43
void ARMTargetAsmStreamer::emitArch(ARM::ArchKind Arch) {
222
43
  OS << "\t.arch\t" << ARM::getArchName(Arch) << "\n";
223
43
}
224
225
3
void ARMTargetAsmStreamer::emitArchExtension(unsigned ArchExt) {
226
3
  OS << "\t.arch_extension\t" << ARM::getArchExtName(ArchExt) << "\n";
227
3
}
228
229
3
void ARMTargetAsmStreamer::emitObjectArch(ARM::ArchKind Arch) {
230
3
  OS << "\t.object_arch\t" << ARM::getArchName(Arch) << '\n';
231
3
}
232
233
1.23k
void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
234
1.23k
  OS << "\t.fpu\t" << ARM::getFPUName(FPU) << "\n";
235
1.23k
}
236
237
2.87k
void ARMTargetAsmStreamer::finishAttributeSection() {}
238
239
void
240
5
ARMTargetAsmStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
241
5
  OS << "\t.tlsdescseq\t" << S->getSymbol().getName();
242
5
}
243
244
11
void ARMTargetAsmStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
245
11
  const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
246
11
247
11
  OS << "\t.thumb_set\t";
248
11
  Symbol->print(OS, MAI);
249
11
  OS << ", ";
250
11
  Value->print(OS, MAI);
251
11
  OS << '\n';
252
11
}
253
254
71
void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
255
71
  OS << "\t.inst";
256
71
  if (Suffix)
257
35
    OS << "." << Suffix;
258
71
  OS << "\t0x" << Twine::utohexstr(Inst) << "\n";
259
71
}
260
261
void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
262
2
                                      const SmallVectorImpl<uint8_t> &Opcodes) {
263
2
  OS << "\t.unwind_raw " << Offset;
264
2
  for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
265
2
                                                OCE = Opcodes.end();
266
4
       OCI != OCE; 
++OCI2
)
267
2
    OS << ", 0x" << Twine::utohexstr(*OCI);
268
2
  OS << '\n';
269
2
}
270
271
class ARMTargetELFStreamer : public ARMTargetStreamer {
272
private:
273
  // This structure holds all attributes, accounting for
274
  // their string/numeric value, so we can later emit them
275
  // in declaration order, keeping all in the same vector
276
  struct AttributeItem {
277
    enum {
278
      HiddenAttribute = 0,
279
      NumericAttribute,
280
      TextAttribute,
281
      NumericAndTextAttributes
282
    } Type;
283
    unsigned Tag;
284
    unsigned IntValue;
285
    std::string StringValue;
286
287
6.88k
    static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
288
6.88k
      // The conformance tag must be emitted first when serialised
289
6.88k
      // into an object file. Specifically, the addenda to the ARM ABI
290
6.88k
      // states that (2.3.7.4):
291
6.88k
      //
292
6.88k
      // "To simplify recognition by consumers in the common case of
293
6.88k
      // claiming conformity for the whole file, this tag should be
294
6.88k
      // emitted first in a file-scope sub-subsection of the first
295
6.88k
      // public subsection of the attributes section."
296
6.88k
      //
297
6.88k
      // So it is special-cased in this comparison predicate when the
298
6.88k
      // attributes are sorted in finishAttributeSection().
299
6.88k
      return (RHS.Tag != ARMBuildAttrs::conformance) &&
300
6.88k
             
(6.60k
(LHS.Tag == ARMBuildAttrs::conformance)6.60k
||
(LHS.Tag < RHS.Tag)6.47k
);
301
6.88k
    }
302
  };
303
304
  StringRef CurrentVendor;
305
  unsigned FPU = ARM::FK_INVALID;
306
  ARM::ArchKind Arch = ARM::ArchKind::INVALID;
307
  ARM::ArchKind EmittedArch = ARM::ArchKind::INVALID;
308
  SmallVector<AttributeItem, 64> Contents;
309
310
  MCSection *AttributeSection = nullptr;
311
312
2.46k
  AttributeItem *getAttributeItem(unsigned Attribute) {
313
19.5k
    for (size_t i = 0; i < Contents.size(); 
++i17.0k
)
314
17.0k
      if (Contents[i].Tag == Attribute)
315
5
        return &Contents[i];
316
2.46k
    
return nullptr2.45k
;
317
2.46k
  }
318
319
  void setAttributeItem(unsigned Attribute, unsigned Value,
320
2.27k
                        bool OverwriteExisting) {
321
2.27k
    // Look for existing attribute item
322
2.27k
    if (AttributeItem *Item = getAttributeItem(Attribute)) {
323
4
      if (!OverwriteExisting)
324
1
        return;
325
3
      Item->Type = AttributeItem::NumericAttribute;
326
3
      Item->IntValue = Value;
327
3
      return;
328
3
    }
329
2.26k
330
2.26k
    // Create new attribute item
331
2.26k
    AttributeItem Item = {
332
2.26k
      AttributeItem::NumericAttribute,
333
2.26k
      Attribute,
334
2.26k
      Value,
335
2.26k
      StringRef("")
336
2.26k
    };
337
2.26k
    Contents.push_back(Item);
338
2.26k
  }
339
340
  void setAttributeItem(unsigned Attribute, StringRef Value,
341
180
                        bool OverwriteExisting) {
342
180
    // Look for existing attribute item
343
180
    if (AttributeItem *Item = getAttributeItem(Attribute)) {
344
1
      if (!OverwriteExisting)
345
0
        return;
346
1
      Item->Type = AttributeItem::TextAttribute;
347
1
      Item->StringValue = Value;
348
1
      return;
349
1
    }
350
179
351
179
    // Create new attribute item
352
179
    AttributeItem Item = {
353
179
      AttributeItem::TextAttribute,
354
179
      Attribute,
355
179
      0,
356
179
      Value
357
179
    };
358
179
    Contents.push_back(Item);
359
179
  }
360
361
  void setAttributeItems(unsigned Attribute, unsigned IntValue,
362
7
                         StringRef StringValue, bool OverwriteExisting) {
363
7
    // Look for existing attribute item
364
7
    if (AttributeItem *Item = getAttributeItem(Attribute)) {
365
0
      if (!OverwriteExisting)
366
0
        return;
367
0
      Item->Type = AttributeItem::NumericAndTextAttributes;
368
0
      Item->IntValue = IntValue;
369
0
      Item->StringValue = StringValue;
370
0
      return;
371
0
    }
372
7
373
7
    // Create new attribute item
374
7
    AttributeItem Item = {
375
7
      AttributeItem::NumericAndTextAttributes,
376
7
      Attribute,
377
7
      IntValue,
378
7
      StringValue
379
7
    };
380
7
    Contents.push_back(Item);
381
7
  }
382
383
  void emitArchDefaultAttributes();
384
  void emitFPUDefaultAttributes();
385
386
  ARMELFStreamer &getStreamer();
387
388
  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;
401
402
  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(ARM::ArchKind Arch) override;
408
  void emitObjectArch(ARM::ArchKind Arch) override;
409
  void emitFPU(unsigned FPU) override;
410
  void emitInst(uint32_t Inst, char Suffix = '\0') override;
411
  void finishAttributeSection() override;
412
  void emitLabel(MCSymbol *Symbol) override;
413
414
  void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
415
  void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
416
417
  size_t calculateContentSize() const;
418
419
  // Reset state between object emissions
420
  void reset() override;
421
422
public:
423
  ARMTargetELFStreamer(MCStreamer &S)
424
457
    : ARMTargetStreamer(S), CurrentVendor("aeabi") {}
425
};
426
427
/// 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!
439
class ARMELFStreamer : public MCELFStreamer {
440
public:
441
  friend class ARMTargetELFStreamer;
442
443
  ARMELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
444
                 std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter,
445
                 bool IsThumb)
446
      : MCELFStreamer(Context, std::move(TAB), std::move(OW), std::move(Emitter)),
447
457
        IsThumb(IsThumb) {
448
457
    EHReset();
449
457
  }
450
451
451
  ~ARMELFStreamer() override = default;
452
453
  void FinishImpl() override;
454
455
  // ARM exception handling directives
456
  void emitFnStart();
457
  void emitFnEnd();
458
  void emitCantUnwind();
459
  void emitPersonality(const MCSymbol *Per);
460
  void emitPersonalityIndex(unsigned index);
461
  void emitHandlerData();
462
  void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
463
  void emitMovSP(unsigned Reg, int64_t Offset = 0);
464
  void emitPad(int64_t Offset);
465
  void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
466
  void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
467
  void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
468
156
                SMLoc Loc) override {
469
156
    EmitDataMappingSymbol();
470
156
    MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);
471
156
  }
472
473
3.04k
  void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
474
3.04k
    LastMappingSymbols[getCurrentSection().first] = std::move(LastEMSInfo);
475
3.04k
    MCELFStreamer::ChangeSection(Section, Subsection);
476
3.04k
    auto LastMappingSymbol = LastMappingSymbols.find(Section);
477
3.04k
    if (LastMappingSymbol != LastMappingSymbols.end()) {
478
1.25k
      LastEMSInfo = std::move(LastMappingSymbol->second);
479
1.25k
      return;
480
1.25k
    }
481
1.78k
    LastEMSInfo.reset(new ElfMappingSymbolInfo(SMLoc(), nullptr, 0));
482
1.78k
  }
483
484
  /// This function is the one used to emit instruction data into the ELF
485
  /// streamer. We override it to add the appropriate mapping symbol if
486
  /// necessary.
487
  void EmitInstruction(const MCInst &Inst,
488
13.0k
                       const MCSubtargetInfo &STI) override {
489
13.0k
    if (IsThumb)
490
8.36k
      EmitThumbMappingSymbol();
491
4.71k
    else
492
4.71k
      EmitARMMappingSymbol();
493
13.0k
494
13.0k
    MCELFStreamer::EmitInstruction(Inst, STI);
495
13.0k
  }
496
497
20
  void emitInst(uint32_t Inst, char Suffix) {
498
20
    unsigned Size;
499
20
    char Buffer[4];
500
20
    const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
501
20
502
20
    switch (Suffix) {
503
20
    case '\0':
504
8
      Size = 4;
505
8
506
8
      assert(!IsThumb);
507
8
      EmitARMMappingSymbol();
508
40
      for (unsigned II = 0, IE = Size; II != IE; 
II++32
) {
509
32
        const unsigned I = LittleEndian ? 
(Size - II - 1)28
:
II4
;
510
32
        Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
511
32
      }
512
8
513
8
      break;
514
20
    case 'n':
515
12
    case 'w':
516
12
      Size = (Suffix == 'n' ? 
25
:
47
);
517
12
518
12
      assert(IsThumb);
519
12
      EmitThumbMappingSymbol();
520
12
      // Thumb wide instructions are emitted as a pair of 16-bit words of the
521
12
      // appropriate endianness.
522
31
      for (unsigned II = 0, IE = Size; II != IE; 
II = II + 219
) {
523
19
        const unsigned I0 = LittleEndian ? 
II + 012
:
II + 17
;
524
19
        const unsigned I1 = LittleEndian ? 
II + 112
:
II + 07
;
525
19
        Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
526
19
        Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
527
19
      }
528
12
529
12
      break;
530
12
    default:
531
0
      llvm_unreachable("Invalid Suffix");
532
20
    }
533
20
534
20
    MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
535
20
  }
536
537
  /// This is one of the functions used to emit data into an ELF section, so the
538
  /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
539
  /// necessary.
540
11.8k
  void EmitBytes(StringRef Data) override {
541
11.8k
    EmitDataMappingSymbol();
542
11.8k
    MCELFStreamer::EmitBytes(Data);
543
11.8k
  }
544
545
968
  void FlushPendingMappingSymbol() {
546
968
    if (!LastEMSInfo->hasInfo())
547
956
      return;
548
12
    ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
549
12
    EmitMappingSymbol("$d", EMS->Loc, EMS->F, EMS->Offset);
550
12
    EMS->resetInfo();
551
12
  }
552
553
  /// This is one of the functions used to emit data into an ELF section, so the
554
  /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
555
  /// necessary.
556
3.66k
  void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
557
3.66k
    if (const MCSymbolRefExpr *SRE = dyn_cast_or_null<MCSymbolRefExpr>(Value)) {
558
1.25k
      if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_SBREL && 
!(Size == 4)16
) {
559
8
        getContext().reportError(Loc, "relocated expression must be 32-bit");
560
8
        return;
561
8
      }
562
1.24k
      getOrCreateDataFragment();
563
1.24k
    }
564
3.66k
565
3.66k
    EmitDataMappingSymbol();
566
3.65k
    MCELFStreamer::EmitValueImpl(Value, Size, Loc);
567
3.65k
  }
568
569
951
  void EmitAssemblerFlag(MCAssemblerFlag Flag) override {
570
951
    MCELFStreamer::EmitAssemblerFlag(Flag);
571
951
572
951
    switch (Flag) {
573
951
    case MCAF_SyntaxUnified:
574
76
      return; // no-op here.
575
951
    case MCAF_Code16:
576
604
      IsThumb = true;
577
604
      return; // Change to Thumb mode
578
951
    case MCAF_Code32:
579
271
      IsThumb = false;
580
271
      return; // Change to ARM mode
581
951
    case MCAF_Code64:
582
0
      return;
583
951
    case MCAF_SubsectionsViaSymbols:
584
0
      return;
585
951
    }
586
951
  }
587
588
private:
589
  enum ElfMappingSymbol {
590
    EMS_None,
591
    EMS_ARM,
592
    EMS_Thumb,
593
    EMS_Data
594
  };
595
596
  struct ElfMappingSymbolInfo {
597
    explicit ElfMappingSymbolInfo(SMLoc Loc, MCFragment *F, uint64_t O)
598
1.78k
        : Loc(Loc), F(F), Offset(O), State(EMS_None) {}
599
12
    void resetInfo() {
600
12
      F = nullptr;
601
12
      Offset = 0;
602
12
    }
603
968
    bool hasInfo() { return F != nullptr; }
604
    SMLoc Loc;
605
    MCFragment *F;
606
    uint64_t Offset;
607
    ElfMappingSymbol State;
608
  };
609
610
15.6k
  void EmitDataMappingSymbol() {
611
15.6k
    if (LastEMSInfo->State == EMS_Data)
612
14.6k
      return;
613
983
    else if (LastEMSInfo->State == EMS_None) {
614
785
      // This is a tentative symbol, it won't really be emitted until it's
615
785
      // actually needed.
616
785
      ElfMappingSymbolInfo *EMS = LastEMSInfo.get();
617
785
      auto *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
618
785
      if (!DF)
619
93
        return;
620
692
      EMS->Loc = SMLoc();
621
692
      EMS->F = getCurrentFragment();
622
692
      EMS->Offset = DF->getContents().size();
623
692
      LastEMSInfo->State = EMS_Data;
624
692
      return;
625
692
    }
626
198
    EmitMappingSymbol("$d");
627
198
    LastEMSInfo->State = EMS_Data;
628
198
  }
629
630
8.37k
  void EmitThumbMappingSymbol() {
631
8.37k
    if (LastEMSInfo->State == EMS_Thumb)
632
7.79k
      return;
633
581
    FlushPendingMappingSymbol();
634
581
    EmitMappingSymbol("$t");
635
581
    LastEMSInfo->State = EMS_Thumb;
636
581
  }
637
638
4.72k
  void EmitARMMappingSymbol() {
639
4.72k
    if (LastEMSInfo->State == EMS_ARM)
640
4.33k
      return;
641
387
    FlushPendingMappingSymbol();
642
387
    EmitMappingSymbol("$a");
643
387
    LastEMSInfo->State = EMS_ARM;
644
387
  }
645
646
1.16k
  void EmitMappingSymbol(StringRef Name) {
647
1.16k
    auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
648
1.16k
        Name + "." + Twine(MappingSymbolCounter++)));
649
1.16k
    EmitLabel(Symbol);
650
1.16k
651
1.16k
    Symbol->setType(ELF::STT_NOTYPE);
652
1.16k
    Symbol->setBinding(ELF::STB_LOCAL);
653
1.16k
    Symbol->setExternal(false);
654
1.16k
  }
655
656
  void EmitMappingSymbol(StringRef Name, SMLoc Loc, MCFragment *F,
657
12
                         uint64_t Offset) {
658
12
    auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
659
12
        Name + "." + Twine(MappingSymbolCounter++)));
660
12
    EmitLabel(Symbol, Loc, F);
661
12
    Symbol->setType(ELF::STT_NOTYPE);
662
12
    Symbol->setBinding(ELF::STB_LOCAL);
663
12
    Symbol->setExternal(false);
664
12
    Symbol->setOffset(Offset);
665
12
  }
666
667
849
  void EmitThumbFunc(MCSymbol *Func) override {
668
849
    getAssembler().setIsThumbFunc(Func);
669
849
    EmitSymbolAttribute(Func, MCSA_ELF_TypeFunction);
670
849
  }
671
672
  // Helper functions for ARM exception handling directives
673
  void EHReset();
674
675
  // Reset state between object emissions
676
  void reset() override;
677
678
  void EmitPersonalityFixup(StringRef Name);
679
  void FlushPendingOffset();
680
  void FlushUnwindOpcodes(bool NoHandlerData);
681
682
  void SwitchToEHSection(StringRef Prefix, unsigned Type, unsigned Flags,
683
                         SectionKind Kind, const MCSymbol &Fn);
684
  void SwitchToExTabSection(const MCSymbol &FnStart);
685
  void SwitchToExIdxSection(const MCSymbol &FnStart);
686
687
  void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
688
689
  bool IsThumb;
690
  int64_t MappingSymbolCounter = 0;
691
692
  DenseMap<const MCSection *, std::unique_ptr<ElfMappingSymbolInfo>>
693
      LastMappingSymbols;
694
695
  std::unique_ptr<ElfMappingSymbolInfo> LastEMSInfo;
696
697
  // ARM Exception Handling Frame Information
698
  MCSymbol *ExTab;
699
  MCSymbol *FnStart;
700
  const MCSymbol *Personality;
701
  unsigned PersonalityIndex;
702
  unsigned FPReg; // Frame pointer register
703
  int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
704
  int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
705
  int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
706
  bool UsedFP;
707
  bool CantUnwind;
708
  SmallVector<uint8_t, 64> Opcodes;
709
  UnwindOpcodeAssembler UnwindOpAsm;
710
};
711
712
} // end anonymous namespace
713
714
7.37k
ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
715
7.37k
  return static_cast<ARMELFStreamer &>(Streamer);
716
7.37k
}
717
718
603
void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
719
601
void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
720
75
void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
721
722
69
void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
723
69
  getStreamer().emitPersonality(Personality);
724
69
}
725
726
6
void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
727
6
  getStreamer().emitPersonalityIndex(Index);
728
6
}
729
730
68
void ARMTargetELFStreamer::emitHandlerData() {
731
68
  getStreamer().emitHandlerData();
732
68
}
733
734
void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
735
104
                                     int64_t Offset) {
736
104
  getStreamer().emitSetFP(FpReg, SpReg, Offset);
737
104
}
738
739
2
void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
740
2
  getStreamer().emitMovSP(Reg, Offset);
741
2
}
742
743
436
void ARMTargetELFStreamer::emitPad(int64_t Offset) {
744
436
  getStreamer().emitPad(Offset);
745
436
}
746
747
void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
748
406
                                       bool isVector) {
749
406
  getStreamer().emitRegSave(RegList, isVector);
750
406
}
751
752
void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
753
53
                                      const SmallVectorImpl<uint8_t> &Opcodes) {
754
53
  getStreamer().emitUnwindRaw(Offset, Opcodes);
755
53
}
756
757
216
void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
758
216
  assert(!Vendor.empty() && "Vendor cannot be empty.");
759
216
760
216
  if (CurrentVendor == Vendor)
761
216
    return;
762
0
763
0
  if (!CurrentVendor.empty())
764
0
    finishAttributeSection();
765
0
766
0
  assert(Contents.empty() &&
767
0
         ".ARM.attributes should be flushed before changing vendor");
768
0
  CurrentVendor = Vendor;
769
0
770
0
}
771
772
1.90k
void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
773
1.90k
  setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
774
1.90k
}
775
776
void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
777
131
                                             StringRef Value) {
778
131
  setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
779
131
}
780
781
void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
782
                                                unsigned IntValue,
783
7
                                                StringRef StringValue) {
784
7
  setAttributeItems(Attribute, IntValue, StringValue,
785
7
                    /* OverwriteExisting= */ true);
786
7
}
787
788
52
void ARMTargetELFStreamer::emitArch(ARM::ArchKind Value) {
789
52
  Arch = Value;
790
52
}
791
792
2
void ARMTargetELFStreamer::emitObjectArch(ARM::ArchKind Value) {
793
2
  EmittedArch = Value;
794
2
}
795
796
49
void ARMTargetELFStreamer::emitArchDefaultAttributes() {
797
49
  using namespace ARMBuildAttrs;
798
49
799
49
  setAttributeItem(CPU_name,
800
49
                   ARM::getCPUAttr(Arch),
801
49
                   false);
802
49
803
49
  if (EmittedArch == ARM::ArchKind::INVALID)
804
47
    setAttributeItem(CPU_arch,
805
47
                     ARM::getArchAttr(Arch),
806
47
                     false);
807
2
  else
808
2
    setAttributeItem(CPU_arch,
809
2
                     ARM::getArchAttr(EmittedArch),
810
2
                     false);
811
49
812
49
  switch (Arch) {
813
49
  case ARM::ArchKind::ARMV2:
814
5
  case ARM::ArchKind::ARMV2A:
815
5
  case ARM::ArchKind::ARMV3:
816
5
  case ARM::ArchKind::ARMV3M:
817
5
  case ARM::ArchKind::ARMV4:
818
5
    setAttributeItem(ARM_ISA_use, Allowed, false);
819
5
    break;
820
5
821
9
  case ARM::ArchKind::ARMV4T:
822
9
  case ARM::ArchKind::ARMV5T:
823
9
  case ARM::ArchKind::ARMV5TE:
824
9
  case ARM::ArchKind::ARMV6:
825
9
    setAttributeItem(ARM_ISA_use, Allowed, false);
826
9
    setAttributeItem(THUMB_ISA_use, Allowed, false);
827
9
    break;
828
9
829
9
  case ARM::ArchKind::ARMV6T2:
830
2
    setAttributeItem(ARM_ISA_use, Allowed, false);
831
2
    setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
832
2
    break;
833
9
834
9
  case ARM::ArchKind::ARMV6K:
835
3
  case ARM::ArchKind::ARMV6KZ:
836
3
    setAttributeItem(ARM_ISA_use, Allowed, false);
837
3
    setAttributeItem(THUMB_ISA_use, Allowed, false);
838
3
    setAttributeItem(Virtualization_use, AllowTZ, false);
839
3
    break;
840
3
841
3
  case ARM::ArchKind::ARMV6M:
842
2
    setAttributeItem(THUMB_ISA_use, Allowed, false);
843
2
    break;
844
3
845
16
  case ARM::ArchKind::ARMV7A:
846
16
    setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
847
16
    setAttributeItem(ARM_ISA_use, Allowed, false);
848
16
    setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
849
16
    break;
850
3
851
3
  case ARM::ArchKind::ARMV7R:
852
2
    setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
853
2
    setAttributeItem(ARM_ISA_use, Allowed, false);
854
2
    setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
855
2
    break;
856
3
857
5
  case ARM::ArchKind::ARMV7EM:
858
5
  case ARM::ArchKind::ARMV7M:
859
5
    setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
860
5
    setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
861
5
    break;
862
5
863
5
  case ARM::ArchKind::ARMV8A:
864
3
  case ARM::ArchKind::ARMV8_1A:
865
3
  case ARM::ArchKind::ARMV8_2A:
866
3
  case ARM::ArchKind::ARMV8_3A:
867
3
  case ARM::ArchKind::ARMV8_4A:
868
3
  case ARM::ArchKind::ARMV8_5A:
869
3
    setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
870
3
    setAttributeItem(ARM_ISA_use, Allowed, false);
871
3
    setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
872
3
    setAttributeItem(MPextension_use, Allowed, false);
873
3
    setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
874
3
    break;
875
3
876
3
  case ARM::ArchKind::ARMV8MBaseline:
877
0
  case ARM::ArchKind::ARMV8MMainline:
878
0
    setAttributeItem(THUMB_ISA_use, AllowThumbDerived, false);
879
0
    setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
880
0
    break;
881
0
882
1
  case ARM::ArchKind::IWMMXT:
883
1
    setAttributeItem(ARM_ISA_use, Allowed, false);
884
1
    setAttributeItem(THUMB_ISA_use, Allowed, false);
885
1
    setAttributeItem(WMMX_arch, AllowWMMXv1, false);
886
1
    break;
887
0
888
1
  case ARM::ArchKind::IWMMXT2:
889
1
    setAttributeItem(ARM_ISA_use, Allowed, false);
890
1
    setAttributeItem(THUMB_ISA_use, Allowed, false);
891
1
    setAttributeItem(WMMX_arch, AllowWMMXv2, false);
892
1
    break;
893
0
894
0
  default:
895
0
    report_fatal_error("Unknown Arch: " + Twine(ARM::getArchName(Arch)));
896
0
    break;
897
49
  }
898
49
}
899
900
129
void ARMTargetELFStreamer::emitFPU(unsigned Value) {
901
129
  FPU = Value;
902
129
}
903
904
104
void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
905
104
  switch (FPU) {
906
104
  case ARM::FK_VFP:
907
0
  case ARM::FK_VFPV2:
908
0
    setAttributeItem(ARMBuildAttrs::FP_arch,
909
0
                     ARMBuildAttrs::AllowFPv2,
910
0
                     /* OverwriteExisting= */ false);
911
0
    break;
912
0
913
3
  case ARM::FK_VFPV3:
914
3
    setAttributeItem(ARMBuildAttrs::FP_arch,
915
3
                     ARMBuildAttrs::AllowFPv3A,
916
3
                     /* OverwriteExisting= */ false);
917
3
    break;
918
0
919
0
  case ARM::FK_VFPV3_FP16:
920
0
    setAttributeItem(ARMBuildAttrs::FP_arch,
921
0
                     ARMBuildAttrs::AllowFPv3A,
922
0
                     /* OverwriteExisting= */ false);
923
0
    setAttributeItem(ARMBuildAttrs::FP_HP_extension,
924
0
                     ARMBuildAttrs::AllowHPFP,
925
0
                     /* OverwriteExisting= */ false);
926
0
    break;
927
0
928
0
  case ARM::FK_VFPV3_D16:
929
0
    setAttributeItem(ARMBuildAttrs::FP_arch,
930
0
                     ARMBuildAttrs::AllowFPv3B,
931
0
                     /* OverwriteExisting= */ false);
932
0
    break;
933
0
934
0
  case ARM::FK_VFPV3_D16_FP16:
935
0
    setAttributeItem(ARMBuildAttrs::FP_arch,
936
0
                     ARMBuildAttrs::AllowFPv3B,
937
0
                     /* OverwriteExisting= */ false);
938
0
    setAttributeItem(ARMBuildAttrs::FP_HP_extension,
939
0
                     ARMBuildAttrs::AllowHPFP,
940
0
                     /* OverwriteExisting= */ false);
941
0
    break;
942
0
943
0
  case ARM::FK_VFPV3XD:
944
0
    setAttributeItem(ARMBuildAttrs::FP_arch,
945
0
                     ARMBuildAttrs::AllowFPv3B,
946
0
                     /* OverwriteExisting= */ false);
947
0
    break;
948
0
  case ARM::FK_VFPV3XD_FP16:
949
0
    setAttributeItem(ARMBuildAttrs::FP_arch,
950
0
                     ARMBuildAttrs::AllowFPv3B,
951
0
                     /* OverwriteExisting= */ false);
952
0
    setAttributeItem(ARMBuildAttrs::FP_HP_extension,
953
0
                     ARMBuildAttrs::AllowHPFP,
954
0
                     /* OverwriteExisting= */ false);
955
0
    break;
956
0
957
1
  case ARM::FK_VFPV4:
958
1
    setAttributeItem(ARMBuildAttrs::FP_arch,
959
1
                     ARMBuildAttrs::AllowFPv4A,
960
1
                     /* OverwriteExisting= */ false);
961
1
    break;
962
0
963
0
  // ABI_HardFP_use is handled in ARMAsmPrinter, so _SP_D16 is treated the same
964
0
  // as _D16 here.
965
0
  case ARM::FK_FPV4_SP_D16:
966
0
  case ARM::FK_VFPV4_D16:
967
0
    setAttributeItem(ARMBuildAttrs::FP_arch,
968
0
                     ARMBuildAttrs::AllowFPv4B,
969
0
                     /* OverwriteExisting= */ false);
970
0
    break;
971
0
972
0
  case ARM::FK_FP_ARMV8:
973
0
    setAttributeItem(ARMBuildAttrs::FP_arch,
974
0
                     ARMBuildAttrs::AllowFPARMv8A,
975
0
                     /* OverwriteExisting= */ false);
976
0
    break;
977
0
978
0
  // FPV5_D16 is identical to FP_ARMV8 except for the number of D registers, so
979
0
  // uses the FP_ARMV8_D16 build attribute.
980
0
  case ARM::FK_FPV5_SP_D16:
981
0
  case ARM::FK_FPV5_D16:
982
0
    setAttributeItem(ARMBuildAttrs::FP_arch,
983
0
                     ARMBuildAttrs::AllowFPARMv8B,
984
0
                     /* OverwriteExisting= */ false);
985
0
    break;
986
0
987
76
  case ARM::FK_NEON:
988
76
    setAttributeItem(ARMBuildAttrs::FP_arch,
989
76
                     ARMBuildAttrs::AllowFPv3A,
990
76
                     /* OverwriteExisting= */ false);
991
76
    setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
992
76
                     ARMBuildAttrs::AllowNeon,
993
76
                     /* OverwriteExisting= */ false);
994
76
    break;
995
0
996
1
  case ARM::FK_NEON_FP16:
997
1
    setAttributeItem(ARMBuildAttrs::FP_arch,
998
1
                     ARMBuildAttrs::AllowFPv3A,
999
1
                     /* OverwriteExisting= */ false);
1000
1
    setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
1001
1
                     ARMBuildAttrs::AllowNeon,
1002
1
                     /* OverwriteExisting= */ false);
1003
1
    setAttributeItem(ARMBuildAttrs::FP_HP_extension,
1004
1
                     ARMBuildAttrs::AllowHPFP,
1005
1
                     /* OverwriteExisting= */ false);
1006
1
    break;
1007
0
1008
18
  case ARM::FK_NEON_VFPV4:
1009
18
    setAttributeItem(ARMBuildAttrs::FP_arch,
1010
18
                     ARMBuildAttrs::AllowFPv4A,
1011
18
                     /* OverwriteExisting= */ false);
1012
18
    setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
1013
18
                     ARMBuildAttrs::AllowNeon2,
1014
18
                     /* OverwriteExisting= */ false);
1015
18
    break;
1016
0
1017
4
  case ARM::FK_NEON_FP_ARMV8:
1018
4
  case ARM::FK_CRYPTO_NEON_FP_ARMV8:
1019
4
    setAttributeItem(ARMBuildAttrs::FP_arch,
1020
4
                     ARMBuildAttrs::AllowFPARMv8A,
1021
4
                     /* OverwriteExisting= */ false);
1022
4
    // 'Advanced_SIMD_arch' must be emitted not here, but within
1023
4
    // ARMAsmPrinter::emitAttributes(), depending on hasV8Ops() and hasV8_1a()
1024
4
    break;
1025
4
1026
4
  case ARM::FK_SOFTVFP:
1027
1
  case ARM::FK_NONE:
1028
1
    break;
1029
1
1030
1
  default:
1031
0
    report_fatal_error("Unknown FPU: " + Twine(FPU));
1032
1
    
break0
;
1033
104
  }
1034
104
}
1035
1036
233
size_t ARMTargetELFStreamer::calculateContentSize() const {
1037
233
  size_t Result = 0;
1038
2.61k
  for (size_t i = 0; i < Contents.size(); 
++i2.38k
) {
1039
2.38k
    AttributeItem item = Contents[i];
1040
2.38k
    switch (item.Type) {
1041
2.38k
    case AttributeItem::HiddenAttribute:
1042
0
      break;
1043
2.38k
    case AttributeItem::NumericAttribute:
1044
2.19k
      Result += getULEB128Size(item.Tag);
1045
2.19k
      Result += getULEB128Size(item.IntValue);
1046
2.19k
      break;
1047
2.38k
    case AttributeItem::TextAttribute:
1048
179
      Result += getULEB128Size(item.Tag);
1049
179
      Result += item.StringValue.size() + 1; // string + '\0'
1050
179
      break;
1051
2.38k
    case AttributeItem::NumericAndTextAttributes:
1052
7
      Result += getULEB128Size(item.Tag);
1053
7
      Result += getULEB128Size(item.IntValue);
1054
7
      Result += item.StringValue.size() + 1; // string + '\0';
1055
7
      break;
1056
2.38k
    }
1057
2.38k
  }
1058
233
  return Result;
1059
233
}
1060
1061
518
void ARMTargetELFStreamer::finishAttributeSection() {
1062
518
  // <format-version>
1063
518
  // [ <section-length> "vendor-name"
1064
518
  // [ <file-tag> <size> <attribute>*
1065
518
  //   | <section-tag> <size> <section-number>* 0 <attribute>*
1066
518
  //   | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
1067
518
  //   ]+
1068
518
  // ]*
1069
518
1070
518
  if (FPU != ARM::FK_INVALID)
1071
104
    emitFPUDefaultAttributes();
1072
518
1073
518
  if (Arch != ARM::ArchKind::INVALID)
1074
49
    emitArchDefaultAttributes();
1075
518
1076
518
  if (Contents.empty())
1077
285
    return;
1078
233
1079
233
  llvm::sort(Contents, AttributeItem::LessTag);
1080
233
1081
233
  ARMELFStreamer &Streamer = getStreamer();
1082
233
1083
233
  // Switch to .ARM.attributes section
1084
233
  if (AttributeSection) {
1085
0
    Streamer.SwitchSection(AttributeSection);
1086
233
  } else {
1087
233
    AttributeSection = Streamer.getContext().getELFSection(
1088
233
        ".ARM.attributes", ELF::SHT_ARM_ATTRIBUTES, 0);
1089
233
    Streamer.SwitchSection(AttributeSection);
1090
233
1091
233
    // Format version
1092
233
    Streamer.EmitIntValue(0x41, 1);
1093
233
  }
1094
233
1095
233
  // Vendor size + Vendor name + '\0'
1096
233
  const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
1097
233
1098
233
  // Tag + Tag Size
1099
233
  const size_t TagHeaderSize = 1 + 4;
1100
233
1101
233
  const size_t ContentsSize = calculateContentSize();
1102
233
1103
233
  Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
1104
233
  Streamer.EmitBytes(CurrentVendor);
1105
233
  Streamer.EmitIntValue(0, 1); // '\0'
1106
233
1107
233
  Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
1108
233
  Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
1109
233
1110
233
  // Size should have been accounted for already, now
1111
233
  // emit each field as its type (ULEB or String)
1112
2.61k
  for (size_t i = 0; i < Contents.size(); 
++i2.38k
) {
1113
2.38k
    AttributeItem item = Contents[i];
1114
2.38k
    Streamer.EmitULEB128IntValue(item.Tag);
1115
2.38k
    switch (item.Type) {
1116
2.38k
    
default: 0
llvm_unreachable0
("Invalid attribute type");
1117
2.38k
    case AttributeItem::NumericAttribute:
1118
2.19k
      Streamer.EmitULEB128IntValue(item.IntValue);
1119
2.19k
      break;
1120
2.38k
    case AttributeItem::TextAttribute:
1121
179
      Streamer.EmitBytes(item.StringValue);
1122
179
      Streamer.EmitIntValue(0, 1); // '\0'
1123
179
      break;
1124
2.38k
    case AttributeItem::NumericAndTextAttributes:
1125
7
      Streamer.EmitULEB128IntValue(item.IntValue);
1126
7
      Streamer.EmitBytes(item.StringValue);
1127
7
      Streamer.EmitIntValue(0, 1); // '\0'
1128
7
      break;
1129
2.38k
    }
1130
2.38k
  }
1131
233
1132
233
  Contents.clear();
1133
233
  FPU = ARM::FK_INVALID;
1134
233
}
1135
1136
4.67k
void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
1137
4.67k
  ARMELFStreamer &Streamer = getStreamer();
1138
4.67k
  if (!Streamer.IsThumb)
1139
2.43k
    return;
1140
2.24k
1141
2.24k
  Streamer.getAssembler().registerSymbol(*Symbol);
1142
2.24k
  unsigned Type = cast<MCSymbolELF>(Symbol)->getType();
1143
2.24k
  if (Type == ELF::STT_FUNC || 
Type == ELF::STT_GNU_IFUNC1.66k
)
1144
578
    Streamer.EmitThumbFunc(Symbol);
1145
2.24k
}
1146
1147
void
1148
3
ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
1149
3
  getStreamer().EmitFixup(S, FK_Data_4);
1150
3
}
1151
1152
8
void ARMTargetELFStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
1153
8
  if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
1154
5
    const MCSymbol &Sym = SRE->getSymbol();
1155
5
    if (!Sym.isDefined()) {
1156
1
      getStreamer().EmitAssignment(Symbol, Value);
1157
1
      return;
1158
1
    }
1159
7
  }
1160
7
1161
7
  getStreamer().EmitThumbFunc(Symbol);
1162
7
  getStreamer().EmitAssignment(Symbol, Value);
1163
7
}
1164
1165
20
void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
1166
20
  getStreamer().emitInst(Inst, Suffix);
1167
20
}
1168
1169
76
void ARMTargetELFStreamer::reset() { AttributeSection = nullptr; }
1170
1171
442
void ARMELFStreamer::FinishImpl() {
1172
442
  MCTargetStreamer &TS = *getTargetStreamer();
1173
442
  ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1174
442
  ATS.finishAttributeSection();
1175
442
1176
442
  MCELFStreamer::FinishImpl();
1177
442
}
1178
1179
76
void ARMELFStreamer::reset() {
1180
76
  MCTargetStreamer &TS = *getTargetStreamer();
1181
76
  ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1182
76
  ATS.reset();
1183
76
  MappingSymbolCounter = 0;
1184
76
  MCELFStreamer::reset();
1185
76
  LastMappingSymbols.clear();
1186
76
  LastEMSInfo.reset();
1187
76
  // MCELFStreamer clear's the assembler's e_flags. However, for
1188
76
  // arm we manually set the ABI version on streamer creation, so
1189
76
  // do the same here
1190
76
  getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1191
76
}
1192
1193
inline void ARMELFStreamer::SwitchToEHSection(StringRef Prefix,
1194
                                              unsigned Type,
1195
                                              unsigned Flags,
1196
                                              SectionKind Kind,
1197
820
                                              const MCSymbol &Fn) {
1198
820
  const MCSectionELF &FnSection =
1199
820
    static_cast<const MCSectionELF &>(Fn.getSection());
1200
820
1201
820
  // Create the name for new section
1202
820
  StringRef FnSecName(FnSection.getSectionName());
1203
820
  SmallString<128> EHSecName(Prefix);
1204
820
  if (FnSecName != ".text") {
1205
210
    EHSecName += FnSecName;
1206
210
  }
1207
820
1208
820
  // Get .ARM.extab or .ARM.exidx section
1209
820
  const MCSymbolELF *Group = FnSection.getGroup();
1210
820
  if (Group)
1211
6
    Flags |= ELF::SHF_GROUP;
1212
820
  MCSectionELF *EHSection = getContext().getELFSection(
1213
820
      EHSecName, Type, Flags, 0, Group, FnSection.getUniqueID(),
1214
820
      static_cast<const MCSymbolELF *>(&Fn));
1215
820
1216
820
  assert(EHSection && "Failed to get the required EH section");
1217
820
1218
820
  // Switch to .ARM.extab or .ARM.exidx section
1219
820
  SwitchSection(EHSection);
1220
820
  EmitCodeAlignment(4);
1221
820
}
1222
1223
219
inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1224
219
  SwitchToEHSection(".ARM.extab", ELF::SHT_PROGBITS, ELF::SHF_ALLOC,
1225
219
                    SectionKind::getData(), FnStart);
1226
219
}
1227
1228
601
inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1229
601
  SwitchToEHSection(".ARM.exidx", ELF::SHT_ARM_EXIDX,
1230
601
                    ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
1231
601
                    SectionKind::getData(), FnStart);
1232
601
}
1233
1234
3
void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1235
3
  MCDataFragment *Frag = getOrCreateDataFragment();
1236
3
  Frag->getFixups().push_back(MCFixup::create(Frag->getContents().size(), Expr,
1237
3
                                              Kind));
1238
3
}
1239
1240
1.05k
void ARMELFStreamer::EHReset() {
1241
1.05k
  ExTab = nullptr;
1242
1.05k
  FnStart = nullptr;
1243
1.05k
  Personality = nullptr;
1244
1.05k
  PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
1245
1.05k
  FPReg = ARM::SP;
1246
1.05k
  FPOffset = 0;
1247
1.05k
  SPOffset = 0;
1248
1.05k
  PendingOffset = 0;
1249
1.05k
  UsedFP = false;
1250
1.05k
  CantUnwind = false;
1251
1.05k
1252
1.05k
  Opcodes.clear();
1253
1.05k
  UnwindOpAsm.Reset();
1254
1.05k
}
1255
1256
603
void ARMELFStreamer::emitFnStart() {
1257
603
  assert(FnStart == nullptr);
1258
603
  FnStart = getContext().createTempSymbol();
1259
603
  EmitLabel(FnStart);
1260
603
}
1261
1262
601
void ARMELFStreamer::emitFnEnd() {
1263
601
  assert(FnStart && ".fnstart must precedes .fnend");
1264
601
1265
601
  // Emit unwind opcodes if there is no .handlerdata directive
1266
601
  if (!ExTab && 
!CantUnwind533
)
1267
458
    FlushUnwindOpcodes(true);
1268
601
1269
601
  // Emit the exception index table entry
1270
601
  SwitchToExIdxSection(*FnStart);
1271
601
1272
601
  if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
1273
457
    EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
1274
601
1275
601
  const MCSymbolRefExpr *FnStartRef =
1276
601
    MCSymbolRefExpr::create(FnStart,
1277
601
                            MCSymbolRefExpr::VK_ARM_PREL31,
1278
601
                            getContext());
1279
601
1280
601
  EmitValue(FnStartRef, 4);
1281
601
1282
601
  if (CantUnwind) {
1283
75
    EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
1284
526
  } else if (ExTab) {
1285
219
    // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
1286
219
    const MCSymbolRefExpr *ExTabEntryRef =
1287
219
      MCSymbolRefExpr::create(ExTab,
1288
219
                              MCSymbolRefExpr::VK_ARM_PREL31,
1289
219
                              getContext());
1290
219
    EmitValue(ExTabEntryRef, 4);
1291
307
  } else {
1292
307
    // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1293
307
    // the second word of exception index table entry.  The size of the unwind
1294
307
    // opcodes should always be 4 bytes.
1295
307
    assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
1296
307
           "Compact model must use __aeabi_unwind_cpp_pr0 as personality");
1297
307
    assert(Opcodes.size() == 4u &&
1298
307
           "Unwind opcode size for __aeabi_unwind_cpp_pr0 must be equal to 4");
1299
307
    uint64_t Intval = Opcodes[0] |
1300
307
                      Opcodes[1] << 8 |
1301
307
                      Opcodes[2] << 16 |
1302
307
                      Opcodes[3] << 24;
1303
307
    EmitIntValue(Intval, Opcodes.size());
1304
307
  }
1305
601
1306
601
  // Switch to the section containing FnStart
1307
601
  SwitchSection(&FnStart->getSection());
1308
601
1309
601
  // Clean exception handling frame information
1310
601
  EHReset();
1311
601
}
1312
1313
75
void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1314
1315
// Add the R_ARM_NONE fixup at the same position
1316
457
void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1317
457
  const MCSymbol *PersonalitySym = getContext().getOrCreateSymbol(Name);
1318
457
1319
457
  const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::create(
1320
457
      PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1321
457
1322
457
  visitUsedExpr(*PersonalityRef);
1323
457
  MCDataFragment *DF = getOrCreateDataFragment();
1324
457
  DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
1325
457
                                            PersonalityRef,
1326
457
                                            MCFixup::getKindForSize(4, false)));
1327
457
}
1328
1329
886
void ARMELFStreamer::FlushPendingOffset() {
1330
886
  if (PendingOffset != 0) {
1331
296
    UnwindOpAsm.EmitSPOffset(-PendingOffset);
1332
296
    PendingOffset = 0;
1333
296
  }
1334
886
}
1335
1336
526
void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
1337
526
  // Emit the unwind opcode to restore $sp.
1338
526
  if (UsedFP) {
1339
101
    const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1340
101
    int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1341
101
    UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
1342
101
    UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1343
425
  } else {
1344
425
    FlushPendingOffset();
1345
425
  }
1346
526
1347
526
  // Finalize the unwind opcode sequence
1348
526
  UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
1349
526
1350
526
  // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1351
526
  // section.  Thus, we don't have to create an entry in the .ARM.extab
1352
526
  // section.
1353
526
  if (NoHandlerData && 
PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0458
)
1354
307
    return;
1355
219
1356
219
  // Switch to .ARM.extab section.
1357
219
  SwitchToExTabSection(*FnStart);
1358
219
1359
219
  // Create .ARM.extab label for offset in .ARM.exidx
1360
219
  assert(!ExTab);
1361
219
  ExTab = getContext().createTempSymbol();
1362
219
  EmitLabel(ExTab);
1363
219
1364
219
  // Emit personality
1365
219
  if (Personality) {
1366
69
    const MCSymbolRefExpr *PersonalityRef =
1367
69
      MCSymbolRefExpr::create(Personality,
1368
69
                              MCSymbolRefExpr::VK_ARM_PREL31,
1369
69
                              getContext());
1370
69
1371
69
    EmitValue(PersonalityRef, 4);
1372
69
  }
1373
219
1374
219
  // Emit unwind opcodes
1375
219
  assert((Opcodes.size() % 4) == 0 &&
1376
219
         "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be multiple of 4");
1377
606
  for (unsigned I = 0; I != Opcodes.size(); 
I += 4387
) {
1378
387
    uint64_t Intval = Opcodes[I] |
1379
387
                      Opcodes[I + 1] << 8 |
1380
387
                      Opcodes[I + 2] << 16 |
1381
387
                      Opcodes[I + 3] << 24;
1382
387
    EmitIntValue(Intval, 4);
1383
387
  }
1384
219
1385
219
  // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1386
219
  // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1387
219
  // after the unwind opcodes.  The handler data consists of several 32-bit
1388
219
  // words, and should be terminated by zero.
1389
219
  //
1390
219
  // In case that the .handlerdata directive is not specified by the
1391
219
  // programmer, we should emit zero to terminate the handler data.
1392
219
  if (NoHandlerData && 
!Personality151
)
1393
148
    EmitIntValue(0, 4);
1394
219
}
1395
1396
68
void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
1397
1398
69
void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
1399
69
  Personality = Per;
1400
69
  UnwindOpAsm.setPersonality(Per);
1401
69
}
1402
1403
6
void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1404
6
  assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1405
6
  PersonalityIndex = Index;
1406
6
}
1407
1408
void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
1409
104
                               int64_t Offset) {
1410
104
  assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
1411
104
         "the operand of .setfp directive should be either $sp or $fp");
1412
104
1413
104
  UsedFP = true;
1414
104
  FPReg = NewFPReg;
1415
104
1416
104
  if (NewSPReg == ARM::SP)
1417
103
    FPOffset = SPOffset + Offset;
1418
1
  else
1419
1
    FPOffset += Offset;
1420
104
}
1421
1422
2
void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
1423
2
  assert((Reg != ARM::SP && Reg != ARM::PC) &&
1424
2
         "the operand of .movsp cannot be either sp or pc");
1425
2
  assert(FPReg == ARM::SP && "current FP must be SP");
1426
2
1427
2
  FlushPendingOffset();
1428
2
1429
2
  FPReg = Reg;
1430
2
  FPOffset = SPOffset + Offset;
1431
2
1432
2
  const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1433
2
  UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1434
2
}
1435
1436
436
void ARMELFStreamer::emitPad(int64_t Offset) {
1437
436
  // Track the change of the $sp offset
1438
436
  SPOffset -= Offset;
1439
436
1440
436
  // To squash multiple .pad directives, we should delay the unwind opcode
1441
436
  // until the .save, .vsave, .handlerdata, or .fnend directives.
1442
436
  PendingOffset -= Offset;
1443
436
}
1444
1445
void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
1446
406
                                 bool IsVector) {
1447
406
  // Collect the registers in the register list
1448
406
  unsigned Count = 0;
1449
406
  uint32_t Mask = 0;
1450
406
  const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1451
1.68k
  for (size_t i = 0; i < RegList.size(); 
++i1.27k
) {
1452
1.27k
    unsigned Reg = MRI->getEncodingValue(RegList[i]);
1453
1.27k
    assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
1454
1.27k
    unsigned Bit = (1u << Reg);
1455
1.27k
    if ((Mask & Bit) == 0) {
1456
1.27k
      Mask |= Bit;
1457
1.27k
      ++Count;
1458
1.27k
    }
1459
1.27k
  }
1460
406
1461
406
  // Track the change the $sp offset: For the .save directive, the
1462
406
  // corresponding push instruction will decrease the $sp by (4 * Count).
1463
406
  // For the .vsave directive, the corresponding vpush instruction will
1464
406
  // decrease $sp by (8 * Count).
1465
406
  SPOffset -= Count * (IsVector ? 
811
:
4395
);
1466
406
1467
406
  // Emit the opcode
1468
406
  FlushPendingOffset();
1469
406
  if (IsVector)
1470
11
    UnwindOpAsm.EmitVFPRegSave(Mask);
1471
395
  else
1472
395
    UnwindOpAsm.EmitRegSave(Mask);
1473
406
}
1474
1475
void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1476
53
                                   const SmallVectorImpl<uint8_t> &Opcodes) {
1477
53
  FlushPendingOffset();
1478
53
  SPOffset = SPOffset - Offset;
1479
53
  UnwindOpAsm.EmitRaw(Opcodes);
1480
53
}
1481
1482
namespace llvm {
1483
1484
MCTargetStreamer *createARMTargetAsmStreamer(MCStreamer &S,
1485
                                             formatted_raw_ostream &OS,
1486
                                             MCInstPrinter *InstPrint,
1487
3.83k
                                             bool isVerboseAsm) {
1488
3.83k
  return new ARMTargetAsmStreamer(S, OS, *InstPrint, isVerboseAsm);
1489
3.83k
}
1490
1491
4
MCTargetStreamer *createARMNullTargetStreamer(MCStreamer &S) {
1492
4
  return new ARMTargetStreamer(S);
1493
4
}
1494
1495
MCTargetStreamer *createARMObjectTargetStreamer(MCStreamer &S,
1496
3.39k
                                                const MCSubtargetInfo &STI) {
1497
3.39k
  const Triple &TT = STI.getTargetTriple();
1498
3.39k
  if (TT.isOSBinFormatELF())
1499
457
    return new ARMTargetELFStreamer(S);
1500
2.93k
  return new ARMTargetStreamer(S);
1501
2.93k
}
1502
1503
MCELFStreamer *createARMELFStreamer(MCContext &Context,
1504
                                    std::unique_ptr<MCAsmBackend> TAB,
1505
                                    std::unique_ptr<MCObjectWriter> OW,
1506
                                    std::unique_ptr<MCCodeEmitter> Emitter,
1507
457
                                    bool RelaxAll, bool IsThumb) {
1508
457
  ARMELFStreamer *S = new ARMELFStreamer(Context, std::move(TAB), std::move(OW),
1509
457
                                         std::move(Emitter), IsThumb);
1510
457
  // FIXME: This should eventually end up somewhere else where more
1511
457
  // intelligent flag decisions can be made. For now we are just maintaining
1512
457
  // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1513
457
  S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1514
457
1515
457
  if (RelaxAll)
1516
0
    S->getAssembler().setRelaxAll(true);
1517
457
  return S;
1518
457
}
1519
1520
} // end namespace llvm