Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFStreamer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- lib/MC/AArch64ELFStreamer.cpp - ELF Object Output for AArch64 ------===//
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 AArch64 ELF .o object files. Different
10
// from generic ELF streamer in emitting mapping symbols ($x and $d) to delimit
11
// regions of data and code.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "AArch64TargetStreamer.h"
16
#include "AArch64WinCOFFStreamer.h"
17
#include "llvm/ADT/DenseMap.h"
18
#include "llvm/ADT/StringRef.h"
19
#include "llvm/ADT/Triple.h"
20
#include "llvm/ADT/Twine.h"
21
#include "llvm/BinaryFormat/ELF.h"
22
#include "llvm/MC/MCAsmBackend.h"
23
#include "llvm/MC/MCAssembler.h"
24
#include "llvm/MC/MCCodeEmitter.h"
25
#include "llvm/MC/MCContext.h"
26
#include "llvm/MC/MCELFStreamer.h"
27
#include "llvm/MC/MCExpr.h"
28
#include "llvm/MC/MCInst.h"
29
#include "llvm/MC/MCObjectWriter.h"
30
#include "llvm/MC/MCSection.h"
31
#include "llvm/MC/MCStreamer.h"
32
#include "llvm/MC/MCSubtargetInfo.h"
33
#include "llvm/MC/MCSymbolELF.h"
34
#include "llvm/MC/MCWinCOFFStreamer.h"
35
#include "llvm/Support/Casting.h"
36
#include "llvm/Support/FormattedStream.h"
37
#include "llvm/Support/raw_ostream.h"
38
39
using namespace llvm;
40
41
namespace {
42
43
class AArch64ELFStreamer;
44
45
class AArch64TargetAsmStreamer : public AArch64TargetStreamer {
46
  formatted_raw_ostream &OS;
47
48
  void emitInst(uint32_t Inst) override;
49
50
public:
51
  AArch64TargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
52
};
53
54
AArch64TargetAsmStreamer::AArch64TargetAsmStreamer(MCStreamer &S,
55
                                                   formatted_raw_ostream &OS)
56
3.43k
  : AArch64TargetStreamer(S), OS(OS) {}
57
58
8
void AArch64TargetAsmStreamer::emitInst(uint32_t Inst) {
59
8
  OS << "\t.inst\t0x" << Twine::utohexstr(Inst) << "\n";
60
8
}
61
62
/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
63
/// the appropriate points in the object files. These symbols are defined in the
64
/// AArch64 ELF ABI:
65
///    infocenter.arm.com/help/topic/com.arm.doc.ihi0056a/IHI0056A_aaelf64.pdf
66
///
67
/// In brief: $x or $d should be emitted at the start of each contiguous region
68
/// of A64 code or data in a section. In practice, this emission does not rely
69
/// on explicit assembler directives but on inherent properties of the
70
/// directives doing the emission (e.g. ".byte" is data, "add x0, x0, x0" an
71
/// instruction).
72
///
73
/// As a result this system is orthogonal to the DataRegion infrastructure used
74
/// by MachO. Beware!
75
class AArch64ELFStreamer : public MCELFStreamer {
76
public:
77
  AArch64ELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
78
                     std::unique_ptr<MCObjectWriter> OW,
79
                     std::unique_ptr<MCCodeEmitter> Emitter)
80
      : MCELFStreamer(Context, std::move(TAB), std::move(OW),
81
                      std::move(Emitter)),
82
1.29k
        MappingSymbolCounter(0), LastEMS(EMS_None) {}
83
84
1.70k
  void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
85
1.70k
    // We have to keep track of the mapping symbol state of any sections we
86
1.70k
    // use. Each one should start off as EMS_None, which is provided as the
87
1.70k
    // default constructor by DenseMap::lookup.
88
1.70k
    LastMappingSymbols[getPreviousSection().first] = LastEMS;
89
1.70k
    LastEMS = LastMappingSymbols.lookup(Section);
90
1.70k
91
1.70k
    MCELFStreamer::ChangeSection(Section, Subsection);
92
1.70k
  }
93
94
  // Reset state between object emissions
95
27
  void reset() override {
96
27
    MappingSymbolCounter = 0;
97
27
    MCELFStreamer::reset();
98
27
    LastMappingSymbols.clear();
99
27
    LastEMS = EMS_None;
100
27
  }
101
102
  /// This function is the one used to emit instruction data into the ELF
103
  /// streamer. We override it to add the appropriate mapping symbol if
104
  /// necessary.
105
  void EmitInstruction(const MCInst &Inst,
106
14.0k
                       const MCSubtargetInfo &STI) override {
107
14.0k
    EmitA64MappingSymbol();
108
14.0k
    MCELFStreamer::EmitInstruction(Inst, STI);
109
14.0k
  }
110
111
  /// Emit a 32-bit value as an instruction. This is only used for the .inst
112
  /// directive, EmitInstruction should be used in other cases.
113
4
  void emitInst(uint32_t Inst) {
114
4
    char Buffer[4];
115
4
116
4
    // We can't just use EmitIntValue here, as that will emit a data mapping
117
4
    // symbol, and swap the endianness on big-endian systems (instructions are
118
4
    // always little-endian).
119
20
    for (unsigned I = 0; I < 4; 
++I16
) {
120
16
      Buffer[I] = uint8_t(Inst);
121
16
      Inst >>= 8;
122
16
    }
123
4
124
4
    EmitA64MappingSymbol();
125
4
    MCELFStreamer::EmitBytes(StringRef(Buffer, 4));
126
4
  }
127
128
  /// This is one of the functions used to emit data into an ELF section, so the
129
  /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
130
  /// if necessary.
131
3.40k
  void EmitBytes(StringRef Data) override {
132
3.40k
    EmitDataMappingSymbol();
133
3.40k
    MCELFStreamer::EmitBytes(Data);
134
3.40k
  }
135
136
  /// This is one of the functions used to emit data into an ELF section, so the
137
  /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
138
  /// if necessary.
139
639
  void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
140
639
    EmitDataMappingSymbol();
141
639
    MCELFStreamer::EmitValueImpl(Value, Size, Loc);
142
639
  }
143
144
  void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
145
101
                                  SMLoc Loc) override {
146
101
    EmitDataMappingSymbol();
147
101
    MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);
148
101
  }
149
private:
150
  enum ElfMappingSymbol {
151
    EMS_None,
152
    EMS_A64,
153
    EMS_Data
154
  };
155
156
4.14k
  void EmitDataMappingSymbol() {
157
4.14k
    if (LastEMS == EMS_Data)
158
3.83k
      return;
159
304
    EmitMappingSymbol("$d");
160
304
    LastEMS = EMS_Data;
161
304
  }
162
163
14.0k
  void EmitA64MappingSymbol() {
164
14.0k
    if (LastEMS == EMS_A64)
165
12.7k
      return;
166
1.32k
    EmitMappingSymbol("$x");
167
1.32k
    LastEMS = EMS_A64;
168
1.32k
  }
169
170
1.62k
  void EmitMappingSymbol(StringRef Name) {
171
1.62k
    auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
172
1.62k
        Name + "." + Twine(MappingSymbolCounter++)));
173
1.62k
    EmitLabel(Symbol);
174
1.62k
    Symbol->setType(ELF::STT_NOTYPE);
175
1.62k
    Symbol->setBinding(ELF::STB_LOCAL);
176
1.62k
    Symbol->setExternal(false);
177
1.62k
  }
178
179
  int64_t MappingSymbolCounter;
180
181
  DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
182
  ElfMappingSymbol LastEMS;
183
};
184
185
} // end anonymous namespace
186
187
namespace llvm {
188
189
4
AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
190
4
  return static_cast<AArch64ELFStreamer &>(Streamer);
191
4
}
192
193
4
void AArch64TargetELFStreamer::emitInst(uint32_t Inst) {
194
4
  getStreamer().emitInst(Inst);
195
4
}
196
197
MCTargetStreamer *createAArch64AsmTargetStreamer(MCStreamer &S,
198
                                                 formatted_raw_ostream &OS,
199
                                                 MCInstPrinter *InstPrint,
200
3.43k
                                                 bool isVerboseAsm) {
201
3.43k
  return new AArch64TargetAsmStreamer(S, OS);
202
3.43k
}
203
204
MCELFStreamer *createAArch64ELFStreamer(MCContext &Context,
205
                                        std::unique_ptr<MCAsmBackend> TAB,
206
                                        std::unique_ptr<MCObjectWriter> OW,
207
                                        std::unique_ptr<MCCodeEmitter> Emitter,
208
1.29k
                                        bool RelaxAll) {
209
1.29k
  AArch64ELFStreamer *S = new AArch64ELFStreamer(
210
1.29k
      Context, std::move(TAB), std::move(OW), std::move(Emitter));
211
1.29k
  if (RelaxAll)
212
0
    S->getAssembler().setRelaxAll(true);
213
1.29k
  return S;
214
1.29k
}
215
216
} // end namespace llvm