diff --git a/llvm/include/llvm/MC/MCObjectFileInfo.h b/llvm/include/llvm/MC/MCObjectFileInfo.h --- a/llvm/include/llvm/MC/MCObjectFileInfo.h +++ b/llvm/include/llvm/MC/MCObjectFileInfo.h @@ -229,6 +229,7 @@ public: void initMCObjectFileInfo(MCContext &MCCtx, bool PIC, bool LargeCodeModel = false); + virtual ~MCObjectFileInfo(); MCContext &getContext() const { return *Ctx; } bool getSupportsWeakOmittedEHFrame() const { @@ -251,6 +252,7 @@ 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 --- a/llvm/lib/MC/MCELFStreamer.cpp +++ b/llvm/lib/MC/MCELFStreamer.cpp @@ -90,7 +90,7 @@ 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 --- a/llvm/lib/MC/MCObjectFileInfo.cpp +++ b/llvm/lib/MC/MCObjectFileInfo.cpp @@ -962,6 +962,8 @@ /* MultiSymbolsAllowed */ true, ".dwmac", XCOFF::SSUBTYP_DWMAC); } +MCObjectFileInfo::~MCObjectFileInfo() = default; + 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 --- a/llvm/lib/Target/RISCV/MCTargetDesc/CMakeLists.txt +++ b/llvm/lib/Target/RISCV/MCTargetDesc/CMakeLists.txt @@ -6,6 +6,7 @@ RISCVMCAsmInfo.cpp RISCVMCCodeEmitter.cpp RISCVMCExpr.cpp + RISCVMCObjectFileInfo.cpp RISCVMCTargetDesc.cpp RISCVMatInt.cpp RISCVTargetStreamer.cpp diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.h new file mode 100644 --- /dev/null +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.h @@ -0,0 +1,28 @@ +//===-- 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 Triple; + +class RISCVMCObjectFileInfo : public MCObjectFileInfo { +public: + unsigned getTextSectionAlignment() const; +}; + +} // namespace llvm + +#endif diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.cpp new file mode 100644 --- /dev/null +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.cpp @@ -0,0 +1,26 @@ +//===-- 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 { + MCContext &Ctx = getContext(); + const MCSubtargetInfo *STI = Ctx.getSubtargetInfo(); + if (STI->hasFeature(RISCV::FeatureStdExtC)) { + return 2; + } + return 4; +} diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp --- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCTargetDesc.cpp @@ -15,12 +15,14 @@ #include "RISCVELFStreamer.h" #include "RISCVInstPrinter.h" #include "RISCVMCAsmInfo.h" +#include "RISCVMCObjectFileInfo.h" #include "RISCVTargetStreamer.h" #include "TargetInfo/RISCVTargetInfo.h" #include "llvm/ADT/STLExtras.h" #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCInstrAnalysis.h" #include "llvm/MC/MCInstrInfo.h" +#include "llvm/MC/MCObjectFileInfo.h" #include "llvm/MC/MCRegisterInfo.h" #include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCSubtargetInfo.h" @@ -62,6 +64,14 @@ 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()) @@ -141,6 +151,7 @@ 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 --- 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