| 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 "AArch64ELFStreamer.h" |
| 16 | #include "AArch64MCTargetDesc.h" |
| 17 | #include "AArch64TargetStreamer.h" |
| 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/StringRef.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/MCELFObjectWriter.h" |
| 27 | #include "llvm/MC/MCELFStreamer.h" |
| 28 | #include "llvm/MC/MCExpr.h" |
| 29 | #include "llvm/MC/MCInst.h" |
| 30 | #include "llvm/MC/MCObjectFileInfo.h" |
| 31 | #include "llvm/MC/MCSectionELF.h" |
| 32 | #include "llvm/MC/MCStreamer.h" |
| 33 | #include "llvm/MC/MCSubtargetInfo.h" |
| 34 | #include "llvm/MC/MCSymbolELF.h" |
| 35 | #include "llvm/MC/MCTargetOptions.h" |
| 36 | #include "llvm/MC/MCWinCOFFStreamer.h" |
| 37 | #include "llvm/Support/AArch64BuildAttributes.h" |
| 38 | #include "llvm/Support/Casting.h" |
| 39 | #include "llvm/Support/FormattedStream.h" |
| 40 | #include "llvm/Support/raw_ostream.h" |
| 41 | |
| 42 | using namespace llvm; |
| 43 | |
| 44 | namespace { |
| 45 | |
| 46 | class AArch64ELFStreamer; |
| 47 | |
| 48 | class AArch64TargetAsmStreamer : public AArch64TargetStreamer { |
| 49 | formatted_raw_ostream &OS; |
| 50 | std::string VendorTag; |
| 51 | |
| 52 | void emitInst(uint32_t Inst) override; |
| 53 | |
| 54 | void emitDirectiveVariantPCS(MCSymbol *Symbol) override { |
| 55 | OS << "\t.variant_pcs\t" << Symbol->getName() << "\n" ; |
| 56 | } |
| 57 | |
| 58 | void emitDirectiveArch(StringRef Name) override { |
| 59 | OS << "\t.arch\t" << Name << "\n" ; |
| 60 | } |
| 61 | |
| 62 | void emitDirectiveArchExtension(StringRef Name) override { |
| 63 | OS << "\t.arch_extension\t" << Name << "\n" ; |
| 64 | } |
| 65 | |
| 66 | void emitARM64WinCFIAllocStack(unsigned Size) override { |
| 67 | OS << "\t.seh_stackalloc\t" << Size << "\n" ; |
| 68 | } |
| 69 | void emitARM64WinCFISaveR19R20X(int Offset) override { |
| 70 | OS << "\t.seh_save_r19r20_x\t" << Offset << "\n" ; |
| 71 | } |
| 72 | void emitARM64WinCFISaveFPLR(int Offset) override { |
| 73 | OS << "\t.seh_save_fplr\t" << Offset << "\n" ; |
| 74 | } |
| 75 | void emitARM64WinCFISaveFPLRX(int Offset) override { |
| 76 | OS << "\t.seh_save_fplr_x\t" << Offset << "\n" ; |
| 77 | } |
| 78 | void emitARM64WinCFISaveReg(unsigned Reg, int Offset) override { |
| 79 | OS << "\t.seh_save_reg\tx" << Reg << ", " << Offset << "\n" ; |
| 80 | } |
| 81 | void emitARM64WinCFISaveRegX(unsigned Reg, int Offset) override { |
| 82 | OS << "\t.seh_save_reg_x\tx" << Reg << ", " << Offset << "\n" ; |
| 83 | } |
| 84 | void emitARM64WinCFISaveRegP(unsigned Reg, int Offset) override { |
| 85 | OS << "\t.seh_save_regp\tx" << Reg << ", " << Offset << "\n" ; |
| 86 | } |
| 87 | void emitARM64WinCFISaveRegPX(unsigned Reg, int Offset) override { |
| 88 | OS << "\t.seh_save_regp_x\tx" << Reg << ", " << Offset << "\n" ; |
| 89 | } |
| 90 | void emitARM64WinCFISaveLRPair(unsigned Reg, int Offset) override { |
| 91 | OS << "\t.seh_save_lrpair\tx" << Reg << ", " << Offset << "\n" ; |
| 92 | } |
| 93 | void emitARM64WinCFISaveFReg(unsigned Reg, int Offset) override { |
| 94 | OS << "\t.seh_save_freg\td" << Reg << ", " << Offset << "\n" ; |
| 95 | } |
| 96 | void emitARM64WinCFISaveFRegX(unsigned Reg, int Offset) override { |
| 97 | OS << "\t.seh_save_freg_x\td" << Reg << ", " << Offset << "\n" ; |
| 98 | } |
| 99 | void emitARM64WinCFISaveFRegP(unsigned Reg, int Offset) override { |
| 100 | OS << "\t.seh_save_fregp\td" << Reg << ", " << Offset << "\n" ; |
| 101 | } |
| 102 | void emitARM64WinCFISaveFRegPX(unsigned Reg, int Offset) override { |
| 103 | OS << "\t.seh_save_fregp_x\td" << Reg << ", " << Offset << "\n" ; |
| 104 | } |
| 105 | void emitARM64WinCFISetFP() override { OS << "\t.seh_set_fp\n" ; } |
| 106 | void emitARM64WinCFIAddFP(unsigned Size) override { |
| 107 | OS << "\t.seh_add_fp\t" << Size << "\n" ; |
| 108 | } |
| 109 | void emitARM64WinCFINop() override { OS << "\t.seh_nop\n" ; } |
| 110 | void emitARM64WinCFISaveNext() override { OS << "\t.seh_save_next\n" ; } |
| 111 | void emitARM64WinCFIPrologEnd() override { OS << "\t.seh_endprologue\n" ; } |
| 112 | void emitARM64WinCFIEpilogStart() override { OS << "\t.seh_startepilogue\n" ; } |
| 113 | void emitARM64WinCFIEpilogEnd() override { OS << "\t.seh_endepilogue\n" ; } |
| 114 | void emitARM64WinCFITrapFrame() override { OS << "\t.seh_trap_frame\n" ; } |
| 115 | void emitARM64WinCFIMachineFrame() override { OS << "\t.seh_pushframe\n" ; } |
| 116 | void emitARM64WinCFIContext() override { OS << "\t.seh_context\n" ; } |
| 117 | void emitARM64WinCFIECContext() override { OS << "\t.seh_ec_context\n" ; } |
| 118 | void emitARM64WinCFIClearUnwoundToCall() override { |
| 119 | OS << "\t.seh_clear_unwound_to_call\n" ; |
| 120 | } |
| 121 | void emitARM64WinCFIPACSignLR() override { |
| 122 | OS << "\t.seh_pac_sign_lr\n" ; |
| 123 | } |
| 124 | |
| 125 | void emitARM64WinCFISaveAnyRegI(unsigned Reg, int Offset) override { |
| 126 | OS << "\t.seh_save_any_reg\tx" << Reg << ", " << Offset << "\n" ; |
| 127 | } |
| 128 | void emitARM64WinCFISaveAnyRegIP(unsigned Reg, int Offset) override { |
| 129 | OS << "\t.seh_save_any_reg_p\tx" << Reg << ", " << Offset << "\n" ; |
| 130 | } |
| 131 | void emitARM64WinCFISaveAnyRegD(unsigned Reg, int Offset) override { |
| 132 | OS << "\t.seh_save_any_reg\td" << Reg << ", " << Offset << "\n" ; |
| 133 | } |
| 134 | void emitARM64WinCFISaveAnyRegDP(unsigned Reg, int Offset) override { |
| 135 | OS << "\t.seh_save_any_reg_p\td" << Reg << ", " << Offset << "\n" ; |
| 136 | } |
| 137 | void emitARM64WinCFISaveAnyRegQ(unsigned Reg, int Offset) override { |
| 138 | OS << "\t.seh_save_any_reg\tq" << Reg << ", " << Offset << "\n" ; |
| 139 | } |
| 140 | void emitARM64WinCFISaveAnyRegQP(unsigned Reg, int Offset) override { |
| 141 | OS << "\t.seh_save_any_reg_p\tq" << Reg << ", " << Offset << "\n" ; |
| 142 | } |
| 143 | void emitARM64WinCFISaveAnyRegIX(unsigned Reg, int Offset) override { |
| 144 | OS << "\t.seh_save_any_reg_x\tx" << Reg << ", " << Offset << "\n" ; |
| 145 | } |
| 146 | void emitARM64WinCFISaveAnyRegIPX(unsigned Reg, int Offset) override { |
| 147 | OS << "\t.seh_save_any_reg_px\tx" << Reg << ", " << Offset << "\n" ; |
| 148 | } |
| 149 | void emitARM64WinCFISaveAnyRegDX(unsigned Reg, int Offset) override { |
| 150 | OS << "\t.seh_save_any_reg_x\td" << Reg << ", " << Offset << "\n" ; |
| 151 | } |
| 152 | void emitARM64WinCFISaveAnyRegDPX(unsigned Reg, int Offset) override { |
| 153 | OS << "\t.seh_save_any_reg_px\td" << Reg << ", " << Offset << "\n" ; |
| 154 | } |
| 155 | void emitARM64WinCFISaveAnyRegQX(unsigned Reg, int Offset) override { |
| 156 | OS << "\t.seh_save_any_reg_x\tq" << Reg << ", " << Offset << "\n" ; |
| 157 | } |
| 158 | void emitARM64WinCFISaveAnyRegQPX(unsigned Reg, int Offset) override { |
| 159 | OS << "\t.seh_save_any_reg_px\tq" << Reg << ", " << Offset << "\n" ; |
| 160 | } |
| 161 | void emitARM64WinCFIAllocZ(int Offset) override { |
| 162 | OS << "\t.seh_allocz\t" << Offset << "\n" ; |
| 163 | } |
| 164 | void emitARM64WinCFISaveZReg(unsigned Reg, int Offset) override { |
| 165 | OS << "\t.seh_save_zreg\tz" << Reg << ", " << Offset << "\n" ; |
| 166 | } |
| 167 | void emitARM64WinCFISavePReg(unsigned Reg, int Offset) override { |
| 168 | OS << "\t.seh_save_preg\tp" << Reg << ", " << Offset << "\n" ; |
| 169 | } |
| 170 | |
| 171 | void emitAttribute(StringRef VendorName, unsigned Tag, unsigned Value, |
| 172 | std::string String) override { |
| 173 | |
| 174 | // AArch64 build attributes for assembly attribute form: |
| 175 | // .aeabi_attribute tag, value |
| 176 | if (unsigned(-1) == Value && "" == String) { |
| 177 | assert(0 && "Arguments error" ); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | unsigned VendorID = AArch64BuildAttributes::getVendorID(Vendor: VendorName); |
| 182 | |
| 183 | switch (VendorID) { |
| 184 | case AArch64BuildAttributes::VENDOR_UNKNOWN: |
| 185 | if (unsigned(-1) != Value) { |
| 186 | OS << "\t.aeabi_attribute" << "\t" << Tag << ", " << Value; |
| 187 | AArch64TargetStreamer::emitAttribute(VendorName, Tag, Value, String: "" ); |
| 188 | } |
| 189 | if ("" != String) { |
| 190 | OS << "\t.aeabi_attribute" << "\t" << Tag << ", " << String; |
| 191 | AArch64TargetStreamer::emitAttribute(VendorName, Tag, Value: unsigned(-1), |
| 192 | String); |
| 193 | } |
| 194 | break; |
| 195 | // Note: AEABI_FEATURE_AND_BITS takes only unsigned values |
| 196 | case AArch64BuildAttributes::AEABI_FEATURE_AND_BITS: |
| 197 | switch (Tag) { |
| 198 | default: // allow emitting any attribute by number |
| 199 | OS << "\t.aeabi_attribute" << "\t" << Tag << ", " << Value; |
| 200 | // Keep the data structure consistent with the case of ELF emission |
| 201 | // (important for llvm-mc asm parsing) |
| 202 | AArch64TargetStreamer::emitAttribute(VendorName, Tag, Value, String: "" ); |
| 203 | break; |
| 204 | case AArch64BuildAttributes::TAG_FEATURE_BTI: |
| 205 | case AArch64BuildAttributes::TAG_FEATURE_GCS: |
| 206 | case AArch64BuildAttributes::TAG_FEATURE_PAC: |
| 207 | OS << "\t.aeabi_attribute" << "\t" << Tag << ", " << Value << "\t// " |
| 208 | << AArch64BuildAttributes::getFeatureAndBitsTagsStr(FeatureAndBitsTag: Tag); |
| 209 | AArch64TargetStreamer::emitAttribute(VendorName, Tag, Value, String: "" ); |
| 210 | break; |
| 211 | } |
| 212 | break; |
| 213 | // Note: AEABI_PAUTHABI takes only unsigned values |
| 214 | case AArch64BuildAttributes::AEABI_PAUTHABI: |
| 215 | switch (Tag) { |
| 216 | default: // allow emitting any attribute by number |
| 217 | OS << "\t.aeabi_attribute" << "\t" << Tag << ", " << Value; |
| 218 | // Keep the data structure consistent with the case of ELF emission |
| 219 | // (important for llvm-mc asm parsing) |
| 220 | AArch64TargetStreamer::emitAttribute(VendorName, Tag, Value, String: "" ); |
| 221 | break; |
| 222 | case AArch64BuildAttributes::TAG_PAUTH_PLATFORM: |
| 223 | case AArch64BuildAttributes::TAG_PAUTH_SCHEMA: |
| 224 | OS << "\t.aeabi_attribute" << "\t" << Tag << ", " << Value << "\t// " |
| 225 | << AArch64BuildAttributes::getPauthABITagsStr(PauthABITag: Tag); |
| 226 | AArch64TargetStreamer::emitAttribute(VendorName, Tag, Value, String: "" ); |
| 227 | break; |
| 228 | } |
| 229 | break; |
| 230 | } |
| 231 | OS << "\n" ; |
| 232 | } |
| 233 | |
| 234 | void emitAttributesSubsection( |
| 235 | StringRef SubsectionName, |
| 236 | AArch64BuildAttributes::SubsectionOptional Optional, |
| 237 | AArch64BuildAttributes::SubsectionType ParameterType) override { |
| 238 | // The AArch64 build attributes assembly subsection header format: |
| 239 | // ".aeabi_subsection name, optional, parameter type" |
| 240 | // optional: required (0) optional (1) |
| 241 | // parameter type: uleb128 or ULEB128 (0) ntbs or NTBS (1) |
| 242 | unsigned SubsectionID = AArch64BuildAttributes::getVendorID(Vendor: SubsectionName); |
| 243 | |
| 244 | assert((0 == Optional || 1 == Optional) && |
| 245 | AArch64BuildAttributes::getSubsectionOptionalUnknownError().data()); |
| 246 | assert((0 == ParameterType || 1 == ParameterType) && |
| 247 | AArch64BuildAttributes::getSubsectionTypeUnknownError().data()); |
| 248 | |
| 249 | std::string SubsectionTag = ".aeabi_subsection" ; |
| 250 | StringRef OptionalStr = getOptionalStr(Optional); |
| 251 | StringRef ParameterStr = getTypeStr(Type: ParameterType); |
| 252 | |
| 253 | switch (SubsectionID) { |
| 254 | case AArch64BuildAttributes::VENDOR_UNKNOWN: { |
| 255 | // Private subsection |
| 256 | break; |
| 257 | } |
| 258 | case AArch64BuildAttributes::AEABI_PAUTHABI: { |
| 259 | assert(AArch64BuildAttributes::REQUIRED == Optional && |
| 260 | "subsection .aeabi-pauthabi should be marked as " |
| 261 | "required and not as optional" ); |
| 262 | assert(AArch64BuildAttributes::ULEB128 == ParameterType && |
| 263 | "subsection .aeabi-pauthabi should be " |
| 264 | "marked as uleb128 and not as ntbs" ); |
| 265 | break; |
| 266 | } |
| 267 | case AArch64BuildAttributes::AEABI_FEATURE_AND_BITS: { |
| 268 | assert(AArch64BuildAttributes::OPTIONAL == Optional && |
| 269 | "subsection .aeabi_feature_and_bits should be " |
| 270 | "marked as optional and not as required" ); |
| 271 | assert(AArch64BuildAttributes::ULEB128 == ParameterType && |
| 272 | "subsection .aeabi_feature_and_bits should " |
| 273 | "be marked as uleb128 and not as ntbs" ); |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | OS << "\t" << SubsectionTag << "\t" << SubsectionName << ", " << OptionalStr |
| 278 | << ", " << ParameterStr; |
| 279 | // Keep the data structure consistent with the case of ELF emission |
| 280 | // (important for llvm-mc asm parsing) |
| 281 | AArch64TargetStreamer::emitAttributesSubsection(VendorName: SubsectionName, IsOptional: Optional, |
| 282 | ParameterType); |
| 283 | OS << "\n" ; |
| 284 | } |
| 285 | |
| 286 | public: |
| 287 | AArch64TargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS); |
| 288 | }; |
| 289 | |
| 290 | AArch64TargetAsmStreamer::AArch64TargetAsmStreamer(MCStreamer &S, |
| 291 | formatted_raw_ostream &OS) |
| 292 | : AArch64TargetStreamer(S), OS(OS) {} |
| 293 | |
| 294 | void AArch64TargetAsmStreamer::emitInst(uint32_t Inst) { |
| 295 | OS << "\t.inst\t0x" << Twine::utohexstr(Val: Inst) << "\n" ; |
| 296 | } |
| 297 | |
| 298 | /// Extend the generic ELFStreamer class so that it can emit mapping symbols at |
| 299 | /// the appropriate points in the object files. These symbols are defined in the |
| 300 | /// AArch64 ELF ABI: |
| 301 | /// infocenter.arm.com/help/topic/com.arm.doc.ihi0056a/IHI0056A_aaelf64.pdf |
| 302 | /// |
| 303 | /// In brief: $x or $d should be emitted at the start of each contiguous region |
| 304 | /// of A64 code or data in a section. In practice, this emission does not rely |
| 305 | /// on explicit assembler directives but on inherent properties of the |
| 306 | /// directives doing the emission (e.g. ".byte" is data, "add x0, x0, x0" an |
| 307 | /// instruction). |
| 308 | /// |
| 309 | /// As a result this system is orthogonal to the DataRegion infrastructure used |
| 310 | /// by MachO. Beware! |
| 311 | class AArch64ELFStreamer : public MCELFStreamer { |
| 312 | public: |
| 313 | friend AArch64TargetELFStreamer; |
| 314 | AArch64ELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB, |
| 315 | std::unique_ptr<MCObjectWriter> OW, |
| 316 | std::unique_ptr<MCCodeEmitter> Emitter) |
| 317 | : MCELFStreamer(Context, std::move(TAB), std::move(OW), |
| 318 | std::move(Emitter)), |
| 319 | LastEMS(EMS_None) { |
| 320 | auto *TO = getContext().getTargetOptions(); |
| 321 | ImplicitMapSyms = TO && TO->ImplicitMapSyms; |
| 322 | } |
| 323 | |
| 324 | void changeSection(MCSection *Section, uint32_t Subsection = 0) override { |
| 325 | // Save the mapping symbol state for potential reuse when revisiting the |
| 326 | // section. When ImplicitMapSyms is true, the initial state is |
| 327 | // EMS_A64 for text sections and EMS_Data for the others. |
| 328 | LastMappingSymbols[getCurrentSection().first] = LastEMS; |
| 329 | auto It = LastMappingSymbols.find(Val: Section); |
| 330 | if (It != LastMappingSymbols.end()) |
| 331 | LastEMS = It->second; |
| 332 | else if (ImplicitMapSyms) |
| 333 | LastEMS = Section->isText() ? EMS_A64 : EMS_Data; |
| 334 | else |
| 335 | LastEMS = EMS_None; |
| 336 | |
| 337 | MCELFStreamer::changeSection(Section, Subsection); |
| 338 | |
| 339 | // Section alignment of 4 to match GNU Assembler |
| 340 | if (Section->isText()) |
| 341 | Section->ensureMinAlignment(MinAlignment: Align(4)); |
| 342 | } |
| 343 | |
| 344 | // Reset state between object emissions |
| 345 | void reset() override { |
| 346 | MCELFStreamer::reset(); |
| 347 | LastMappingSymbols.clear(); |
| 348 | LastEMS = EMS_None; |
| 349 | } |
| 350 | |
| 351 | /// This function is the one used to emit instruction data into the ELF |
| 352 | /// streamer. We override it to add the appropriate mapping symbol if |
| 353 | /// necessary. |
| 354 | void emitInstruction(const MCInst &Inst, |
| 355 | const MCSubtargetInfo &STI) override { |
| 356 | emitA64MappingSymbol(); |
| 357 | MCELFStreamer::emitInstruction(Inst, STI); |
| 358 | } |
| 359 | |
| 360 | /// Emit a 32-bit value as an instruction. This is only used for the .inst |
| 361 | /// directive, EmitInstruction should be used in other cases. |
| 362 | void emitInst(uint32_t Inst) { |
| 363 | char Buffer[4]; |
| 364 | |
| 365 | // We can't just use EmitIntValue here, as that will emit a data mapping |
| 366 | // symbol, and swap the endianness on big-endian systems (instructions are |
| 367 | // always little-endian). |
| 368 | for (char &C : Buffer) { |
| 369 | C = uint8_t(Inst); |
| 370 | Inst >>= 8; |
| 371 | } |
| 372 | |
| 373 | emitA64MappingSymbol(); |
| 374 | MCELFStreamer::emitBytes(Data: StringRef(Buffer, 4)); |
| 375 | } |
| 376 | |
| 377 | /// This is one of the functions used to emit data into an ELF section, so the |
| 378 | /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d) |
| 379 | /// if necessary. |
| 380 | void emitBytes(StringRef Data) override { |
| 381 | emitDataMappingSymbol(); |
| 382 | MCELFStreamer::emitBytes(Data); |
| 383 | } |
| 384 | |
| 385 | /// This is one of the functions used to emit data into an ELF section, so the |
| 386 | /// AArch64 streamer overrides it to add the appropriate mapping symbol ($d) |
| 387 | /// if necessary. |
| 388 | void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) override { |
| 389 | emitDataMappingSymbol(); |
| 390 | MCELFStreamer::emitValueImpl(Value, Size, Loc); |
| 391 | } |
| 392 | |
| 393 | void emitFill(const MCExpr &NumBytes, uint64_t FillValue, |
| 394 | SMLoc Loc) override { |
| 395 | emitDataMappingSymbol(); |
| 396 | MCObjectStreamer::emitFill(NumBytes, FillValue, Loc); |
| 397 | } |
| 398 | |
| 399 | private: |
| 400 | enum ElfMappingSymbol { |
| 401 | EMS_None, |
| 402 | EMS_A64, |
| 403 | EMS_Data |
| 404 | }; |
| 405 | |
| 406 | void emitDataMappingSymbol() { |
| 407 | if (LastEMS == EMS_Data) |
| 408 | return; |
| 409 | emitMappingSymbol(Name: "$d" ); |
| 410 | LastEMS = EMS_Data; |
| 411 | } |
| 412 | |
| 413 | void emitA64MappingSymbol() { |
| 414 | if (LastEMS == EMS_A64) |
| 415 | return; |
| 416 | emitMappingSymbol(Name: "$x" ); |
| 417 | LastEMS = EMS_A64; |
| 418 | } |
| 419 | |
| 420 | MCSymbol *emitMappingSymbol(StringRef Name) { |
| 421 | auto *Symbol = cast<MCSymbolELF>(Val: getContext().createLocalSymbol(Name)); |
| 422 | emitLabel(Symbol); |
| 423 | return Symbol; |
| 424 | } |
| 425 | |
| 426 | DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols; |
| 427 | ElfMappingSymbol LastEMS; |
| 428 | bool ImplicitMapSyms; |
| 429 | }; |
| 430 | } // end anonymous namespace |
| 431 | |
| 432 | AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() { |
| 433 | return static_cast<AArch64ELFStreamer &>(Streamer); |
| 434 | } |
| 435 | |
| 436 | void AArch64TargetELFStreamer::emitAttributesSubsection( |
| 437 | StringRef VendorName, AArch64BuildAttributes::SubsectionOptional IsOptional, |
| 438 | AArch64BuildAttributes::SubsectionType ParameterType) { |
| 439 | AArch64TargetStreamer::emitAttributesSubsection(VendorName, IsOptional, |
| 440 | ParameterType); |
| 441 | } |
| 442 | |
| 443 | void AArch64TargetELFStreamer::emitAttribute(StringRef VendorName, unsigned Tag, |
| 444 | unsigned Value, |
| 445 | std::string String) { |
| 446 | if (unsigned(-1) != Value) |
| 447 | AArch64TargetStreamer::emitAttribute(VendorName, Tag, Value, String: "" ); |
| 448 | if ("" != String) |
| 449 | AArch64TargetStreamer::emitAttribute(VendorName, Tag, Value: unsigned(-1), String); |
| 450 | } |
| 451 | |
| 452 | void AArch64TargetELFStreamer::emitInst(uint32_t Inst) { |
| 453 | getStreamer().emitInst(Inst); |
| 454 | } |
| 455 | |
| 456 | void AArch64TargetELFStreamer::emitDirectiveVariantPCS(MCSymbol *Symbol) { |
| 457 | getStreamer().getAssembler().registerSymbol(Symbol: *Symbol); |
| 458 | cast<MCSymbolELF>(Val: Symbol)->setOther(ELF::STO_AARCH64_VARIANT_PCS); |
| 459 | } |
| 460 | |
| 461 | void AArch64TargetELFStreamer::finish() { |
| 462 | AArch64TargetStreamer::finish(); |
| 463 | AArch64ELFStreamer &S = getStreamer(); |
| 464 | MCContext &Ctx = S.getContext(); |
| 465 | auto &Asm = S.getAssembler(); |
| 466 | |
| 467 | S.emitAttributesSection(AttributeSection, Section: ".ARM.attributes" , |
| 468 | Type: ELF::SHT_AARCH64_ATTRIBUTES, SubSectionVec&: AttributeSubSections); |
| 469 | |
| 470 | // If ImplicitMapSyms is specified, ensure that text sections end with |
| 471 | // the A64 state while non-text sections end with the data state. When |
| 472 | // sections are combined by the linker, the subsequent section will start with |
| 473 | // the right state. The ending mapping symbol is added right after the last |
| 474 | // symbol relative to the section. When a dumb linker combines (.text.0; .word |
| 475 | // 0) and (.text.1; .word 0), the ending $x of .text.0 precedes the $d of |
| 476 | // .text.1, even if they have the same address. |
| 477 | if (S.ImplicitMapSyms) { |
| 478 | auto &Syms = Asm.getSymbols(); |
| 479 | const size_t NumSyms = Syms.size(); |
| 480 | DenseMap<MCSection *, std::pair<size_t, MCSymbol *>> EndMapSym; |
| 481 | for (MCSection &Sec : Asm) { |
| 482 | S.switchSection(Section: &Sec); |
| 483 | if (S.LastEMS == (Sec.isText() ? AArch64ELFStreamer::EMS_Data |
| 484 | : AArch64ELFStreamer::EMS_A64)) |
| 485 | EndMapSym.insert( |
| 486 | KV: {&Sec, {NumSyms, S.emitMappingSymbol(Name: Sec.isText() ? "$x" : "$d" )}}); |
| 487 | } |
| 488 | if (Syms.size() != NumSyms) { |
| 489 | SmallVector<const MCSymbol *, 0> NewSyms; |
| 490 | Syms.truncate(N: NumSyms); |
| 491 | // Find the last symbol index for each candidate section. |
| 492 | for (auto [I, Sym] : llvm::enumerate(First&: Syms)) { |
| 493 | if (!Sym->isInSection()) |
| 494 | continue; |
| 495 | auto It = EndMapSym.find(Val: &Sym->getSection()); |
| 496 | if (It != EndMapSym.end()) |
| 497 | It->second.first = I; |
| 498 | } |
| 499 | SmallVector<size_t, 0> Idx; |
| 500 | for (auto [I, Sym] : llvm::enumerate(First&: Syms)) { |
| 501 | NewSyms.push_back(Elt: Sym); |
| 502 | if (!Sym->isInSection()) |
| 503 | continue; |
| 504 | auto It = EndMapSym.find(Val: &Sym->getSection()); |
| 505 | // If `Sym` is the last symbol relative to the section, add the ending |
| 506 | // mapping symbol after `Sym`. |
| 507 | if (It != EndMapSym.end() && I == It->second.first) { |
| 508 | NewSyms.push_back(Elt: It->second.second); |
| 509 | Idx.push_back(Elt: I); |
| 510 | } |
| 511 | } |
| 512 | Syms = std::move(NewSyms); |
| 513 | // F.second holds the number of symbols added before the FILE symbol. |
| 514 | // Take into account the inserted mapping symbols. |
| 515 | for (auto &F : S.getWriter().getFileNames()) |
| 516 | F.second += llvm::lower_bound(Range&: Idx, Value&: F.second) - Idx.begin(); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | // The mix of execute-only and non-execute-only at link time is |
| 521 | // non-execute-only. To avoid the empty implicitly created .text |
| 522 | // section from making the whole .text section non-execute-only, we |
| 523 | // mark it execute-only if it is empty and there is at least one |
| 524 | // execute-only section in the object. |
| 525 | if (any_of(Range&: Asm, P: [](const MCSection &Sec) { |
| 526 | return cast<MCSectionELF>(Val: Sec).getFlags() & ELF::SHF_AARCH64_PURECODE; |
| 527 | })) { |
| 528 | auto *Text = |
| 529 | static_cast<MCSectionELF *>(Ctx.getObjectFileInfo()->getTextSection()); |
| 530 | bool Empty = true; |
| 531 | for (auto &F : *Text) { |
| 532 | if (auto *DF = dyn_cast<MCDataFragment>(Val: &F)) { |
| 533 | if (!DF->getContents().empty()) { |
| 534 | Empty = false; |
| 535 | break; |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | if (Empty) |
| 540 | Text->setFlags(Text->getFlags() | ELF::SHF_AARCH64_PURECODE); |
| 541 | } |
| 542 | |
| 543 | MCSectionELF *MemtagSec = nullptr; |
| 544 | for (const MCSymbol &Symbol : Asm.symbols()) { |
| 545 | const auto &Sym = cast<MCSymbolELF>(Val: Symbol); |
| 546 | if (Sym.isMemtag()) { |
| 547 | MemtagSec = Ctx.getELFSection(Section: ".memtag.globals.static" , |
| 548 | Type: ELF::SHT_AARCH64_MEMTAG_GLOBALS_STATIC, Flags: 0); |
| 549 | break; |
| 550 | } |
| 551 | } |
| 552 | if (!MemtagSec) |
| 553 | return; |
| 554 | |
| 555 | // switchSection registers the section symbol and invalidates symbols(). We |
| 556 | // need a separate symbols() loop. |
| 557 | S.switchSection(Section: MemtagSec); |
| 558 | const auto *Zero = MCConstantExpr::create(Value: 0, Ctx); |
| 559 | for (const MCSymbol &Symbol : Asm.symbols()) { |
| 560 | const auto &Sym = cast<MCSymbolELF>(Val: Symbol); |
| 561 | if (!Sym.isMemtag()) |
| 562 | continue; |
| 563 | auto *SRE = MCSymbolRefExpr::create(Symbol: &Sym, Ctx); |
| 564 | (void)S.emitRelocDirective(Offset: *Zero, Name: "BFD_RELOC_NONE" , Expr: SRE, Loc: SMLoc(), |
| 565 | STI: *Ctx.getSubtargetInfo()); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | MCTargetStreamer * |
| 570 | llvm::createAArch64AsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS, |
| 571 | MCInstPrinter *InstPrint) { |
| 572 | return new AArch64TargetAsmStreamer(S, OS); |
| 573 | } |
| 574 | |
| 575 | MCStreamer * |
| 576 | llvm::createAArch64ELFStreamer(const Triple &, MCContext &Context, |
| 577 | std::unique_ptr<MCAsmBackend> &&TAB, |
| 578 | std::unique_ptr<MCObjectWriter> &&OW, |
| 579 | std::unique_ptr<MCCodeEmitter> &&Emitter) { |
| 580 | return new AArch64ELFStreamer(Context, std::move(TAB), std::move(OW), |
| 581 | std::move(Emitter)); |
| 582 | } |
| 583 | |