2014-05-24 20:50:23 +08:00
|
|
|
//===- lib/MC/AArch64ELFStreamer.cpp - ELF Object Output for AArch64 ------===//
|
2014-03-29 18:18:08 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file assembles .s files and emits AArch64 ELF .o object files. Different
|
|
|
|
// from generic ELF streamer in emitting mapping symbols ($x and $d) to delimit
|
|
|
|
// regions of data and code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-05-24 00:15:10 +08:00
|
|
|
#include "AArch64TargetStreamer.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/MC/MCELFStreamer.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2014-10-23 04:35:57 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/MC/MCAsmBackend.h"
|
2014-10-23 04:35:57 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/MC/MCAssembler.h"
|
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCELFStreamer.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCObjectStreamer.h"
|
|
|
|
#include "llvm/MC/MCSection.h"
|
|
|
|
#include "llvm/MC/MCSectionELF.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2015-06-03 04:38:46 +08:00
|
|
|
#include "llvm/MC/MCSymbolELF.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/MC/MCValue.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ELF.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2014-10-23 04:35:57 +08:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-10-23 04:35:57 +08:00
|
|
|
class AArch64ELFStreamer;
|
|
|
|
|
|
|
|
class AArch64TargetAsmStreamer : public AArch64TargetStreamer {
|
|
|
|
formatted_raw_ostream &OS;
|
|
|
|
|
|
|
|
void emitInst(uint32_t Inst) override;
|
|
|
|
|
|
|
|
public:
|
|
|
|
AArch64TargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
|
|
|
|
};
|
|
|
|
|
|
|
|
AArch64TargetAsmStreamer::AArch64TargetAsmStreamer(MCStreamer &S,
|
|
|
|
formatted_raw_ostream &OS)
|
|
|
|
: AArch64TargetStreamer(S), OS(OS) {}
|
|
|
|
|
|
|
|
void AArch64TargetAsmStreamer::emitInst(uint32_t Inst) {
|
2015-05-24 00:39:10 +08:00
|
|
|
OS << "\t.inst\t0x" << Twine::utohexstr(Inst) << "\n";
|
2014-10-23 04:35:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class AArch64TargetELFStreamer : public AArch64TargetStreamer {
|
|
|
|
private:
|
|
|
|
AArch64ELFStreamer &getStreamer();
|
|
|
|
|
|
|
|
void emitInst(uint32_t Inst) override;
|
|
|
|
|
|
|
|
public:
|
|
|
|
AArch64TargetELFStreamer(MCStreamer &S) : AArch64TargetStreamer(S) {}
|
|
|
|
};
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
|
|
|
|
/// the appropriate points in the object files. These symbols are defined in the
|
|
|
|
/// AArch64 ELF ABI:
|
|
|
|
/// infocenter.arm.com/help/topic/com.arm.doc.ihi0056a/IHI0056A_aaelf64.pdf
|
|
|
|
///
|
|
|
|
/// In brief: $x or $d should be emitted at the start of each contiguous region
|
|
|
|
/// of A64 code or data in a section. In practice, this emission does not rely
|
|
|
|
/// on explicit assembler directives but on inherent properties of the
|
|
|
|
/// directives doing the emission (e.g. ".byte" is data, "add x0, x0, x0" an
|
|
|
|
/// instruction).
|
|
|
|
///
|
|
|
|
/// As a result this system is orthogonal to the DataRegion infrastructure used
|
|
|
|
/// by MachO. Beware!
|
2014-05-24 20:50:23 +08:00
|
|
|
class AArch64ELFStreamer : public MCELFStreamer {
|
2014-03-29 18:18:08 +08:00
|
|
|
public:
|
2014-10-23 04:35:57 +08:00
|
|
|
friend class AArch64TargetELFStreamer;
|
|
|
|
|
2015-04-15 06:14:34 +08:00
|
|
|
AArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
|
|
|
|
raw_pwrite_stream &OS, MCCodeEmitter *Emitter)
|
2014-03-29 18:18:08 +08:00
|
|
|
: MCELFStreamer(Context, TAB, OS, Emitter), MappingSymbolCounter(0),
|
|
|
|
LastEMS(EMS_None) {}
|
|
|
|
|
2015-05-22 03:20:38 +08:00
|
|
|
void ChangeSection(MCSection *Section, const MCExpr *Subsection) override {
|
2014-03-29 18:18:08 +08:00
|
|
|
// We have to keep track of the mapping symbol state of any sections we
|
|
|
|
// use. Each one should start off as EMS_None, which is provided as the
|
|
|
|
// default constructor by DenseMap::lookup.
|
|
|
|
LastMappingSymbols[getPreviousSection().first] = LastEMS;
|
|
|
|
LastEMS = LastMappingSymbols.lookup(Section);
|
|
|
|
|
|
|
|
MCELFStreamer::ChangeSection(Section, Subsection);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function is the one used to emit instruction data into the ELF
|
|
|
|
/// streamer. We override it to add the appropriate mapping symbol if
|
|
|
|
/// necessary.
|
2014-04-29 15:58:25 +08:00
|
|
|
void EmitInstruction(const MCInst &Inst,
|
|
|
|
const MCSubtargetInfo &STI) override {
|
2014-03-29 18:18:08 +08:00
|
|
|
EmitA64MappingSymbol();
|
|
|
|
MCELFStreamer::EmitInstruction(Inst, STI);
|
|
|
|
}
|
|
|
|
|
2016-01-20 20:54:31 +08:00
|
|
|
/// Emit a 32-bit value as an instruction. This is only used for the .inst
|
|
|
|
/// directive, EmitInstruction should be used in other cases.
|
2014-10-23 04:35:57 +08:00
|
|
|
void emitInst(uint32_t Inst) {
|
2016-01-20 20:54:31 +08:00
|
|
|
char Buffer[4];
|
|
|
|
|
|
|
|
// We can't just use EmitIntValue here, as that will emit a data mapping
|
|
|
|
// symbol, and swap the endianness on big-endian systems (instructions are
|
|
|
|
// always little-endian).
|
|
|
|
for (unsigned I = 0; I < 4; ++I) {
|
|
|
|
Buffer[I] = uint8_t(Inst);
|
|
|
|
Inst >>= 8;
|
|
|
|
}
|
|
|
|
|
2014-10-23 04:35:57 +08:00
|
|
|
EmitA64MappingSymbol();
|
2016-01-20 20:54:31 +08:00
|
|
|
MCELFStreamer::EmitBytes(StringRef(Buffer, 4));
|
2014-10-23 04:35:57 +08:00
|
|
|
}
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
/// This is one of the functions used to emit data into an ELF section, so the
|
2014-05-24 20:50:23 +08:00
|
|
|
/// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
|
2014-03-29 18:18:08 +08:00
|
|
|
/// if necessary.
|
2014-04-29 15:58:25 +08:00
|
|
|
void EmitBytes(StringRef Data) override {
|
2014-03-29 18:18:08 +08:00
|
|
|
EmitDataMappingSymbol();
|
|
|
|
MCELFStreamer::EmitBytes(Data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is one of the functions used to emit data into an ELF section, so the
|
2014-05-24 20:50:23 +08:00
|
|
|
/// AArch64 streamer overrides it to add the appropriate mapping symbol ($d)
|
2014-03-29 18:18:08 +08:00
|
|
|
/// if necessary.
|
2015-09-21 07:35:59 +08:00
|
|
|
void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override {
|
2014-03-29 18:18:08 +08:00
|
|
|
EmitDataMappingSymbol();
|
2015-11-17 00:22:47 +08:00
|
|
|
MCELFStreamer::EmitValueImpl(Value, Size, Loc);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum ElfMappingSymbol {
|
|
|
|
EMS_None,
|
|
|
|
EMS_A64,
|
|
|
|
EMS_Data
|
|
|
|
};
|
|
|
|
|
|
|
|
void EmitDataMappingSymbol() {
|
|
|
|
if (LastEMS == EMS_Data)
|
|
|
|
return;
|
|
|
|
EmitMappingSymbol("$d");
|
|
|
|
LastEMS = EMS_Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitA64MappingSymbol() {
|
|
|
|
if (LastEMS == EMS_A64)
|
|
|
|
return;
|
|
|
|
EmitMappingSymbol("$x");
|
|
|
|
LastEMS = EMS_A64;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitMappingSymbol(StringRef Name) {
|
2015-06-03 04:38:46 +08:00
|
|
|
auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol(
|
|
|
|
Name + "." + Twine(MappingSymbolCounter++)));
|
2015-06-26 19:31:13 +08:00
|
|
|
EmitLabel(Symbol);
|
2015-06-03 04:38:46 +08:00
|
|
|
Symbol->setType(ELF::STT_NOTYPE);
|
|
|
|
Symbol->setBinding(ELF::STB_LOCAL);
|
2015-05-30 05:45:01 +08:00
|
|
|
Symbol->setExternal(false);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t MappingSymbolCounter;
|
|
|
|
|
|
|
|
DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
|
|
|
|
ElfMappingSymbol LastEMS;
|
|
|
|
};
|
2014-10-23 04:35:57 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
|
|
|
|
return static_cast<AArch64ELFStreamer &>(Streamer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AArch64TargetELFStreamer::emitInst(uint32_t Inst) {
|
|
|
|
getStreamer().emitInst(Inst);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
2015-03-17 05:43:42 +08:00
|
|
|
MCTargetStreamer *createAArch64AsmTargetStreamer(MCStreamer &S,
|
|
|
|
formatted_raw_ostream &OS,
|
|
|
|
MCInstPrinter *InstPrint,
|
|
|
|
bool isVerboseAsm) {
|
|
|
|
return new AArch64TargetAsmStreamer(S, OS);
|
2014-10-23 04:35:57 +08:00
|
|
|
}
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
MCELFStreamer *createAArch64ELFStreamer(MCContext &Context, MCAsmBackend &TAB,
|
2015-04-15 06:14:34 +08:00
|
|
|
raw_pwrite_stream &OS,
|
|
|
|
MCCodeEmitter *Emitter, bool RelaxAll) {
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64ELFStreamer *S = new AArch64ELFStreamer(Context, TAB, OS, Emitter);
|
2014-03-29 18:18:08 +08:00
|
|
|
if (RelaxAll)
|
|
|
|
S->getAssembler().setRelaxAll(true);
|
|
|
|
return S;
|
|
|
|
}
|
2015-03-19 09:50:16 +08:00
|
|
|
|
|
|
|
MCTargetStreamer *
|
|
|
|
createAArch64ObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
|
2015-09-16 00:17:27 +08:00
|
|
|
const Triple &TT = STI.getTargetTriple();
|
2015-07-07 00:33:18 +08:00
|
|
|
if (TT.isOSBinFormatELF())
|
2015-03-19 09:50:16 +08:00
|
|
|
return new AArch64TargetELFStreamer(S);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|