From 67f5503adc0fe9d886a33df58f49a4b3e2b8c21e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Mon, 27 Dec 2021 14:12:28 +0900 Subject: [PATCH] Apply llvm.patch Taken from https://github.com/dotnet/runtime/blob/7ab969c84ef05ba948c0075392716ce335b47744/src/coreclr/tools/aot/ObjWriter/llvm.patch. --- llvm/include/llvm/MC/MCObjectStreamer.h | 5 +++ llvm/include/llvm/MC/MCStreamer.h | 4 ++ llvm/lib/MC/MCObjectStreamer.cpp | 9 ++++- llvm/lib/MC/MCStreamer.cpp | 1 + llvm/lib/MC/WinCOFFObjectWriter.cpp | 4 +- .../MCTargetDesc/AArch64AsmBackend.cpp | 11 +++++- .../MCTargetDesc/AArch64ELFObjectWriter.cpp | 1 + .../AArch64WinCOFFObjectWriter.cpp | 3 ++ .../Target/ARM/MCTargetDesc/ARMAsmBackend.cpp | 5 +++ .../ARM/MCTargetDesc/ARMELFObjectWriter.cpp | 2 + .../ARM/MCTargetDesc/ARMELFStreamer.cpp | 37 ++++++++++++++++++- .../ARM/MCTargetDesc/ARMTargetStreamer.cpp | 1 + 12 files changed, 77 insertions(+), 6 deletions(-) diff --git a/llvm/include/llvm/MC/MCObjectStreamer.h b/llvm/include/llvm/MC/MCObjectStreamer.h index a00000bc11b60c..810fe68fc5bf35 100644 --- a/llvm/include/llvm/MC/MCObjectStreamer.h +++ b/llvm/include/llvm/MC/MCObjectStreamer.h @@ -120,6 +120,11 @@ class MCObjectStreamer : public MCStreamer { void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override; void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc = SMLoc()) override; + /// \brief EmitValueImpl with additional param, that allows to emit PCRelative + /// MCFixup. + void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc, + bool isPCRelative); + void emitULEB128Value(const MCExpr *Value) override; void emitSLEB128Value(const MCExpr *Value) override; void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override; diff --git a/llvm/include/llvm/MC/MCStreamer.h b/llvm/include/llvm/MC/MCStreamer.h index cdc728f7377207..8457b465e807bf 100644 --- a/llvm/include/llvm/MC/MCStreamer.h +++ b/llvm/include/llvm/MC/MCStreamer.h @@ -146,6 +146,7 @@ class ARMTargetStreamer : public MCTargetStreamer { virtual void emitPad(int64_t Offset); virtual void emitRegSave(const SmallVectorImpl &RegList, bool isVector); + virtual void emitLsda(const MCSymbol *Symbol); virtual void emitUnwindRaw(int64_t StackOffset, const SmallVectorImpl &Opcodes); @@ -669,6 +670,9 @@ class MCStreamer { /// etc. virtual void emitBytes(StringRef Data); + /// \brief Emit the given \p Instruction data into the current section. + virtual void emitInstructionBytes(StringRef Data); + /// Functionally identical to EmitBytes. When emitting textual assembly, this /// method uses .byte directives instead of .ascii or .asciz for readability. virtual void emitBinaryData(StringRef Data); diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp index 1c23d31f8744a6..1c7012bd9b7736 100644 --- a/llvm/lib/MC/MCObjectStreamer.cpp +++ b/llvm/lib/MC/MCObjectStreamer.cpp @@ -223,7 +223,7 @@ void MCObjectStreamer::emitCFISections(bool EH, bool Debug) { } void MCObjectStreamer::emitValueImpl(const MCExpr *Value, unsigned Size, - SMLoc Loc) { + SMLoc Loc, bool isPCRelative) { MCStreamer::emitValueImpl(Value, Size, Loc); MCDataFragment *DF = getOrCreateDataFragment(); flushPendingLabels(DF, DF->getContents().size()); @@ -243,10 +243,15 @@ void MCObjectStreamer::emitValueImpl(const MCExpr *Value, unsigned Size, } DF->getFixups().push_back( MCFixup::create(DF->getContents().size(), Value, - MCFixup::getKindForSize(Size, false), Loc)); + MCFixup::getKindForSize(Size, isPCRelative), Loc)); DF->getContents().resize(DF->getContents().size() + Size, 0); } +void MCObjectStreamer::emitValueImpl(const MCExpr *Value, unsigned Size, + SMLoc Loc) { + emitValueImpl(Value, Size, Loc, false); +} + MCSymbol *MCObjectStreamer::emitCFILabel() { MCSymbol *Label = getContext().createTempSymbol("cfi"); emitLabel(Label); diff --git a/llvm/lib/MC/MCStreamer.cpp b/llvm/lib/MC/MCStreamer.cpp index 4b5ae3cc202de9..623e06fa6ba889 100644 --- a/llvm/lib/MC/MCStreamer.cpp +++ b/llvm/lib/MC/MCStreamer.cpp @@ -1136,6 +1136,7 @@ void MCStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, void MCStreamer::changeSection(MCSection *, const MCExpr *) {} void MCStreamer::emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {} void MCStreamer::emitBytes(StringRef Data) {} +void MCStreamer::emitInstructionBytes(StringRef Data) { emitBytes(Data); } void MCStreamer::emitBinaryData(StringRef Data) { emitBytes(Data); } void MCStreamer::emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) { visitUsedExpr(*Value); diff --git a/llvm/lib/MC/WinCOFFObjectWriter.cpp b/llvm/lib/MC/WinCOFFObjectWriter.cpp index 901d2c06e716f0..932f487f7dcd61 100644 --- a/llvm/lib/MC/WinCOFFObjectWriter.cpp +++ b/llvm/lib/MC/WinCOFFObjectWriter.cpp @@ -794,7 +794,9 @@ void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm, if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 && Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) || (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 && - Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32)) + Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32) || + (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARM64 && + Reloc.Data.Type == COFF::IMAGE_REL_ARM64_REL32)) FixedValue += 4; if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) { diff --git a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp index 75a9f2f5c80e3b..73a2d7b2c5bf96 100644 --- a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp +++ b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp @@ -141,6 +141,7 @@ static unsigned getFixupKindNumBytes(unsigned Kind) { case AArch64::fixup_aarch64_pcrel_call26: case FK_Data_4: case FK_SecRel_4: + case FK_PCRel_4: return 4; case FK_Data_8: @@ -330,13 +331,19 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target, case FK_Data_8: case FK_SecRel_2: case FK_SecRel_4: + case FK_PCRel_4: return Value; } } Optional AArch64AsmBackend::getFixupKind(StringRef Name) const { - if (!TheTriple.isOSBinFormatELF()) - return None; + if (!TheTriple.isOSBinFormatELF()) { + return StringSwitch>(Name) + .Case("R_AARCH64_CALL26", (MCFixupKind)AArch64::fixup_aarch64_pcrel_call26) + .Case("R_AARCH64_ADR_PREL_PG_HI21", (MCFixupKind)AArch64::fixup_aarch64_pcrel_adrp_imm21) + .Case("R_AARCH64_ADD_ABS_LO12_NC", (MCFixupKind)AArch64::fixup_aarch64_add_imm12) + .Default(MCAsmBackend::getFixupKind(Name)); + } unsigned Type = llvm::StringSwitch(Name) #define ELF_RELOC(X, Y) .Case(#X, Y) diff --git a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp index fcf67bd2f740f5..c6bcf963f6f89f 100644 --- a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp +++ b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp @@ -130,6 +130,7 @@ unsigned AArch64ELFObjectWriter::getRelocType(MCContext &Ctx, return ELF::R_AARCH64_NONE; case FK_Data_2: return R_CLS(PREL16); + case FK_PCRel_4: case FK_Data_4: { return Target.getAccessVariant() == MCSymbolRefExpr::VK_PLT ? R_CLS(PLT32) diff --git a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64WinCOFFObjectWriter.cpp b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64WinCOFFObjectWriter.cpp index aaadc8dc1b6001..48ed1e63f6d9bd 100644 --- a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64WinCOFFObjectWriter.cpp +++ b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64WinCOFFObjectWriter.cpp @@ -91,6 +91,9 @@ unsigned AArch64WinCOFFObjectWriter::getRelocType( case FK_Data_8: return COFF::IMAGE_REL_ARM64_ADDR64; + case FK_PCRel_4: + return COFF::IMAGE_REL_ARM64_REL32; + case FK_SecRel_2: return COFF::IMAGE_REL_ARM64_SECTION; diff --git a/llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp b/llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp index b02aef3c338b8f..f9636aed5068c8 100644 --- a/llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp +++ b/llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp @@ -448,6 +448,8 @@ unsigned ARMAsmBackend::adjustFixupValue(const MCAssembler &Asm, case FK_Data_2: case FK_Data_4: return Value; + case FK_PCRel_4: + return Value; case FK_SecRel_2: return Value; case FK_SecRel_4: @@ -967,6 +969,9 @@ static unsigned getFixupKindNumBytes(unsigned Kind) { case ARM::fixup_le: return 4; + case FK_PCRel_4: + return 4; + case FK_SecRel_2: return 2; case FK_SecRel_4: diff --git a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp index 37d81e4b0af13d..00a543cc08a966 100644 --- a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp +++ b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp @@ -111,6 +111,8 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target, case MCSymbolRefExpr::VK_ARM_PREL31: return ELF::R_ARM_PREL31; } + case FK_PCRel_4: + return ELF::R_ARM_REL32; case ARM::fixup_arm_blx: case ARM::fixup_arm_uncondbl: switch (Modifier) { diff --git a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp index 07ca5c29f0ec99..fa9f4981824031 100644 --- a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp +++ b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp @@ -84,6 +84,7 @@ class ARMTargetAsmStreamer : public ARMTargetStreamer { void emitPad(int64_t Offset) override; void emitRegSave(const SmallVectorImpl &RegList, bool isVector) override; + void emitLsda(const MCSymbol* Symbol) override; void emitUnwindRaw(int64_t Offset, const SmallVectorImpl &Opcodes) override; @@ -272,6 +273,8 @@ void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset, OS << '\n'; } +void ARMTargetAsmStreamer::emitLsda(const MCSymbol* Symbol) {} + class ARMTargetELFStreamer : public ARMTargetStreamer { private: // This structure holds all attributes, accounting for @@ -388,6 +391,7 @@ class ARMTargetELFStreamer : public ARMTargetStreamer { void emitPad(int64_t Offset) override; void emitRegSave(const SmallVectorImpl &RegList, bool isVector) override; + void emitLsda(const MCSymbol *Symbol) override; void emitUnwindRaw(int64_t Offset, const SmallVectorImpl &Opcodes) override; @@ -457,6 +461,7 @@ class ARMELFStreamer : public MCELFStreamer { void emitMovSP(unsigned Reg, int64_t Offset = 0); void emitPad(int64_t Offset); void emitRegSave(const SmallVectorImpl &RegList, bool isVector); + void emitLsda(const MCSymbol* Symbol); void emitUnwindRaw(int64_t Offset, const SmallVectorImpl &Opcodes); void emitFill(const MCExpr &NumBytes, uint64_t FillValue, SMLoc Loc) override { @@ -536,6 +541,18 @@ class ARMELFStreamer : public MCELFStreamer { MCELFStreamer::emitBytes(Data); } + /// 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. + void emitInstructionBytes(StringRef Data) override { + if (IsThumb) + EmitThumbMappingSymbol(); + else + EmitARMMappingSymbol(); + + MCELFStreamer::emitBytes(Data); + } + void FlushPendingMappingSymbol() { if (!LastEMSInfo->hasInfo()) return; @@ -699,6 +716,7 @@ class ARMELFStreamer : public MCELFStreamer { bool CantUnwind; SmallVector Opcodes; UnwindOpcodeAssembler UnwindOpAsm; + const MCSymbol *Lsda; }; } // end anonymous namespace @@ -741,6 +759,10 @@ void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl &RegList, getStreamer().emitRegSave(RegList, isVector); } +void ARMTargetELFStreamer::emitLsda(const MCSymbol *Symbol) { + getStreamer().emitLsda(Symbol); +} + void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset, const SmallVectorImpl &Opcodes) { getStreamer().emitUnwindRaw(Offset, Opcodes); @@ -1241,6 +1263,7 @@ void ARMELFStreamer::EHReset() { PendingOffset = 0; UsedFP = false; CantUnwind = false; + Lsda = nullptr; Opcodes.clear(); UnwindOpAsm.Reset(); @@ -1343,6 +1366,8 @@ void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) { } // Finalize the unwind opcode sequence + if (Lsda != nullptr && Opcodes.size() <= 4u) + PersonalityIndex = ARM::EHABI::AEABI_UNWIND_CPP_PR1; UnwindOpAsm.Finalize(PersonalityIndex, Opcodes); // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx @@ -1387,7 +1412,13 @@ void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) { // // In case that the .handlerdata directive is not specified by the // programmer, we should emit zero to terminate the handler data. - if (NoHandlerData && !Personality) + if (Lsda != nullptr) { + const MCSymbolRefExpr *LsdaRef = + MCSymbolRefExpr::create(Lsda, + MCSymbolRefExpr::VK_None, + getContext()); + emitValue(LsdaRef, 4); + } else if (NoHandlerData && !Personality) emitInt32(0); } @@ -1470,6 +1501,10 @@ void ARMELFStreamer::emitRegSave(const SmallVectorImpl &RegList, UnwindOpAsm.EmitRegSave(Mask); } +void ARMELFStreamer::emitLsda(const MCSymbol *Symbol) { + Lsda = Symbol; +} + void ARMELFStreamer::emitUnwindRaw(int64_t Offset, const SmallVectorImpl &Opcodes) { FlushPendingOffset(); diff --git a/llvm/lib/Target/ARM/MCTargetDesc/ARMTargetStreamer.cpp b/llvm/lib/Target/ARM/MCTargetDesc/ARMTargetStreamer.cpp index 1fee354cad9326..7d2eb8ef5e3673 100644 --- a/llvm/lib/Target/ARM/MCTargetDesc/ARMTargetStreamer.cpp +++ b/llvm/lib/Target/ARM/MCTargetDesc/ARMTargetStreamer.cpp @@ -97,6 +97,7 @@ void ARMTargetStreamer::emitMovSP(unsigned Reg, int64_t Offset) {} void ARMTargetStreamer::emitPad(int64_t Offset) {} void ARMTargetStreamer::emitRegSave(const SmallVectorImpl &RegList, bool isVector) {} +void ARMTargetStreamer::emitLsda(const MCSymbol *Symbol) {} void ARMTargetStreamer::emitUnwindRaw(int64_t StackOffset, const SmallVectorImpl &Opcodes) { }