diff --git a/llvm/include/llvm/MC/MCObjectFileInfo.h b/llvm/include/llvm/MC/MCObjectFileInfo.h index 1bdae0bc541a..ba7450ac64f1 100644 --- a/llvm/include/llvm/MC/MCObjectFileInfo.h +++ b/llvm/include/llvm/MC/MCObjectFileInfo.h @@ -231,6 +231,7 @@ protected: public: void initMCObjectFileInfo(MCContext &MCCtx, bool PIC, bool LargeCodeModel = false); + virtual ~MCObjectFileInfo(); MCContext &getContext() const { return *Ctx; } bool getSupportsWeakOmittedEHFrame() const { @@ -253,6 +254,7 @@ public: return CompactUnwindDwarfEHFrameOnly; } + virtual unsigned getTextSectionAlignment() const { return 4; } MCSection *getTextSection() const { return TextSection; } MCSection *getDataSection() const { return DataSection; } MCSection *getBSSSection() const { return BSSSection; } diff --git a/llvm/lib/MC/MCELFStreamer.cpp b/llvm/lib/MC/MCELFStreamer.cpp index f29282ff705a..2371eaba8d00 100644 --- a/llvm/lib/MC/MCELFStreamer.cpp +++ b/llvm/lib/MC/MCELFStreamer.cpp @@ -91,7 +91,7 @@ void MCELFStreamer::mergeFragment(MCDataFragment *DF, void MCELFStreamer::InitSections(bool NoExecStack) { MCContext &Ctx = getContext(); SwitchSection(Ctx.getObjectFileInfo()->getTextSection()); - emitCodeAlignment(4); + emitCodeAlignment(Ctx.getObjectFileInfo()->getTextSectionAlignment()); if (NoExecStack) SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx)); diff --git a/llvm/lib/MC/MCObjectFileInfo.cpp b/llvm/lib/MC/MCObjectFileInfo.cpp index bebbc9d83f6a..d7f85f793c55 100644 --- a/llvm/lib/MC/MCObjectFileInfo.cpp +++ b/llvm/lib/MC/MCObjectFileInfo.cpp @@ -981,6 +981,8 @@ void MCObjectFileInfo::initXCOFFMCObjectFileInfo(const Triple &T) { /* MultiSymbolsAllowed */ true, ".dwmac", XCOFF::SSUBTYP_DWMAC); } +MCObjectFileInfo::~MCObjectFileInfo() {} + void MCObjectFileInfo::initMCObjectFileInfo(MCContext &MCCtx, bool PIC, bool LargeCodeModel) { PositionIndependent = PIC; diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/CMakeLists.txt b/llvm/lib/Target/RISCV/MCTargetDesc/CMakeLists.txt index 5f2928c921e1..a0326d8021f5 100644 --- a/llvm/lib/Target/RISCV/MCTargetDesc/CMakeLists.txt +++ b/llvm/lib/Target/RISCV/MCTargetDesc/CMakeLists.txt @@ -6,6 +6,7 @@ add_llvm_component_library(LLVMRISCVDesc RISCVMCAsmInfo.cpp RISCVMCCodeEmitter.cpp RISCVMCExpr.cpp + RISCVMCObjectFileInfo.cpp RISCVMCTargetDesc.cpp RISCVMatInt.cpp RISCVTargetStreamer.cpp diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.cpp new file mode 100644 index 000000000000..9c9d9221578c --- /dev/null +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.cpp @@ -0,0 +1,22 @@ +//===-- RISCVMCObjectFileInfo.cpp - RISCV object file properties ----------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file contains the declarations of the RISCVMCObjectFileInfo properties. +// +//===----------------------------------------------------------------------===// + +#include "RISCVMCObjectFileInfo.h" +#include "RISCVMCTargetDesc.h" +#include "llvm/MC/MCContext.h" + +using namespace llvm; + +unsigned RISCVMCObjectFileInfo::getTextSectionAlignment() const { + const MCSubtargetInfo *STI = getContext().getSubtargetInfo(); + return STI->hasFeature(RISCV::FeatureStdExtC) ? 2 : 4; +} diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.h new file mode 100644 index 000000000000..2f6b10229864 --- /dev/null +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.h @@ -0,0 +1,27 @@ +//===-- RISCVMCObjectFileInfo.h - RISCV object file Info -------*- C++ -*--===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file contains the declaration of the RISCVMCObjectFileInfo class. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVMCOBJECTFILEINFO_H +#define LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVMCOBJECTFILEINFO_H + +#include "llvm/MC/MCObjectFileInfo.h" + +namespace llvm { + +class RISCVMCObjectFileInfo : public MCObjectFileInfo { +public: + unsigned getTextSectionAlignment() const override; +}; + +} // namespace llvm + +#endif diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp index 38c32539833c..a73ba6918e27 100644 --- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp @@ -15,6 +15,7 @@ #include "RISCVELFStreamer.h" #include "RISCVInstPrinter.h" #include "RISCVMCAsmInfo.h" +#include "RISCVMCObjectFileInfo.h" #include "RISCVTargetStreamer.h" #include "TargetInfo/RISCVTargetInfo.h" #include "llvm/ADT/STLExtras.h" @@ -23,6 +24,7 @@ #include "llvm/MC/MCCodeEmitter.h" #include "llvm/MC/MCInstrAnalysis.h" #include "llvm/MC/MCInstrInfo.h" +#include "llvm/MC/MCObjectFileInfo.h" #include "llvm/MC/MCObjectWriter.h" #include "llvm/MC/MCRegisterInfo.h" #include "llvm/MC/MCStreamer.h" @@ -65,6 +67,14 @@ static MCAsmInfo *createRISCVMCAsmInfo(const MCRegisterInfo &MRI, return MAI; } +static MCObjectFileInfo * +createRISCVMCObjectFileInfo(MCContext &Ctx, bool PIC, + bool LargeCodeModel = false) { + MCObjectFileInfo *MOFI = new RISCVMCObjectFileInfo(); + MOFI->initMCObjectFileInfo(Ctx, PIC, LargeCodeModel); + return MOFI; +} + static MCSubtargetInfo *createRISCVMCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS) { if (CPU.empty()) @@ -155,6 +165,7 @@ MCStreamer *createRISCVELFStreamer(const Triple &T, MCContext &Context, extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTargetMC() { for (Target *T : {&getTheRISCV32Target(), &getTheRISCV64Target()}) { TargetRegistry::RegisterMCAsmInfo(*T, createRISCVMCAsmInfo); + TargetRegistry::RegisterMCObjectFileInfo(*T, createRISCVMCObjectFileInfo); TargetRegistry::RegisterMCInstrInfo(*T, createRISCVMCInstrInfo); TargetRegistry::RegisterMCRegInfo(*T, createRISCVMCRegisterInfo); TargetRegistry::RegisterMCAsmBackend(*T, createRISCVAsmBackend); diff --git a/llvm/test/MC/RISCV/align.s b/llvm/test/MC/RISCV/align.s index 5b9f3dc8baa9..804effb6600b 100644 --- a/llvm/test/MC/RISCV/align.s +++ b/llvm/test/MC/RISCV/align.s @@ -33,13 +33,15 @@ # Linker could satisfy alignment by removing NOPs after linker relaxation. # The first R_RISCV_ALIGN come from -# MCELFStreamer::InitSections() emitCodeAlignment(4). +# MCELFStreamer::InitSections() emitCodeAlignment(getTextSectionAligntment()). # C-EXT-RELAX-RELOC: R_RISCV_ALIGN - 0x2 # C-EXT-RELAX-INST: c.nop test: .p2align 2 -# C-EXT-RELAX-RELOC: R_RISCV_ALIGN - 0x2 -# C-EXT-RELAX-INST: c.nop +# If the +c extension is enabled, the text section will be 2-byte aligned, so +# one c.nop instruction is sufficient. +# C-EXT-RELAX-RELOC-NOT: R_RISCV_ALIGN - 0x2 +# C-EXT-RELAX-INST-NOT: c.nop bne zero, a0, .LBB0_2 mv a0, zero .p2align 3