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 : AArch64TargetStreamer(S), OS(OS) {} 57 58 void AArch64TargetAsmStreamer::emitInst(uint32_t Inst) { 59 OS << "\t.inst\t0x" << Twine::utohexstr(Inst) << "\n"; 60 } 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 MappingSymbolCounter(0), LastEMS(EMS_None) {} 83 84 void ChangeSection(MCSection *Section, const MCExpr *Subsection) override { 85 // We have to keep track of the mapping symbol state of any sections we 86 // use. Each one should start off as EMS_None, which is provided as the 87 // default constructor by DenseMap::lookup. 88 LastMappingSymbols[getPreviousSection().first] = LastEMS; 89 LastEMS = LastMappingSymbols.lookup(Section); 90 91 MCELFStreamer::ChangeSection(Section, Subsection); 92 } 93 94 // Reset state between object emissions 95 void reset() override { 96 MappingSymbolCounter = 0; 97 MCELFStreamer::reset(); 98 LastMappingSymbols.clear(); 99 LastEMS = EMS_None; 100 } 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 const MCSubtargetInfo &STI) override { 107 EmitA64MappingSymbol(); 108 MCELFStreamer::EmitInstruction(Inst, STI); 109 } 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 void emitInst(uint32_t Inst) { 114 char Buffer[4]; 115 116 // We can't just use EmitIntValue here, as that will emit a data mapping 117 // symbol, and swap the endianness on big-endian systems (instructions are 118 // always little-endian). 119 for (unsigned I = 0; I < 4; ++I) { 120 Buffer[I] = uint8_t(Inst); 121 Inst >>= 8; 122 } 123 124 EmitA64MappingSymbol(); 125 MCELFStreamer::EmitBytes(StringRef(Buffer, 4)); 126 } 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 void EmitBytes(StringRef Data) override { 132 EmitDataMappingSymbol(); 133 MCELFStreamer::EmitBytes(Data); 134 } 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 void EmitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override { 140 EmitDataMappingSymbol(); 141 MCELFStreamer::EmitValueImpl(Value, Size, Loc); 142 } 143 144 void emitFill(const MCExpr &NumBytes, uint64_t FillValue, 145 SMLoc Loc) override { 146 EmitDataMappingSymbol(); 147 MCObjectStreamer::emitFill(NumBytes, FillValue, Loc); 148 } 149 private: 150 enum ElfMappingSymbol { 151 EMS_None, 152 EMS_A64, 153 EMS_Data 154 }; 155 156 void EmitDataMappingSymbol() { 157 if (LastEMS == EMS_Data) 158 return; 159 EmitMappingSymbol("$d"); 160 LastEMS = EMS_Data; 161 } 162 163 void EmitA64MappingSymbol() { 164 if (LastEMS == EMS_A64) 165 return; 166 EmitMappingSymbol("$x"); 167 LastEMS = EMS_A64; 168 } 169 170 void EmitMappingSymbol(StringRef Name) { 171 auto *Symbol = cast<MCSymbolELF>(getContext().getOrCreateSymbol( 172 Name + "." + Twine(MappingSymbolCounter++))); 173 EmitLabel(Symbol); 174 Symbol->setType(ELF::STT_NOTYPE); 175 Symbol->setBinding(ELF::STB_LOCAL); 176 Symbol->setExternal(false); 177 } 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 AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() { 190 return static_cast<AArch64ELFStreamer &>(Streamer); 191 } 192 193 void AArch64TargetELFStreamer::emitInst(uint32_t Inst) { 194 getStreamer().emitInst(Inst); 195 } 196 197 MCTargetStreamer *createAArch64AsmTargetStreamer(MCStreamer &S, 198 formatted_raw_ostream &OS, 199 MCInstPrinter *InstPrint, 200 bool isVerboseAsm) { 201 return new AArch64TargetAsmStreamer(S, OS); 202 } 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 bool RelaxAll) { 209 AArch64ELFStreamer *S = new AArch64ELFStreamer( 210 Context, std::move(TAB), std::move(OW), std::move(Emitter)); 211 if (RelaxAll) 212 S->getAssembler().setRelaxAll(true); 213 return S; 214 } 215 216 } // end namespace llvm 217